Monday, October 27, 2008

Ruby Observer PartI

before observable

   1  #!/usr/local/bin/ruby

   2  # encoding: utf-8

   3  class Ticker

   4    def start

   5      loop do

   6        price = StockPrice.price

   7        print_price(price)

   8        print warn_price(price)

   9        sleep 1

  10      end

  11    end

  12    def print_price(price)

  13      printf "%.02f\n", price

  14    end

  15    def warn_price(price)

  16      if price > 140

  17        "High\n"

  18      elsif price < 60

  19        "Low\n"

  20      end

  21    end

  22  end

  23  class StockPrice

  24    def self.price

  25      (rand * 100) + 50

  26    end

  27  end

  28  ticker = Ticker.new

  29  ticker.start

No comments: