Created
December 6, 2011 13:41
-
-
Save ka8725/1438240 to your computer and use it in GitHub Desktop.
Orange tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrangeTree | |
attr_reader :height # the same as: def height @height; end | |
attr_reader :count_oranges # the same as: def count_oranges @count_oranges; end | |
def initialize(count_live_years = 10, grow_inches_per_year = 1) | |
@grow_inches_per_year = grow_inches_per_year | |
@count_live_years = count_live_years | |
@count_oranges = 0 | |
@age = 0 | |
@height = 0 | |
end | |
def one_year_passes | |
if @age <= @count_live_years | |
@age += 1 | |
@height += @grow_inches_per_year | |
@count_oranges = @age / 2 # New oranges growed and old failed down after 1 year | |
else | |
puts "Tree was died" | |
end | |
end | |
def pick_an_orange | |
if count_oranges > 0 | |
@count_oranges -= 1 | |
puts "Picked one orange. It was very tasty!" | |
else | |
puts "There are no oranges on the tree to pick in this year" | |
end | |
end | |
end | |
tree = OrangeTree.new(10, 2) | |
11.times do |i| | |
puts "Count years passed: #{i}" | |
puts "Heigth for this tree is: #{tree.height} inches" | |
puts "Count oranges for this year: #{tree.count_oranges}" | |
(tree.count_oranges + 1) .times { tree.pick_an_orange } | |
tree.one_year_passes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment