Skip to content

Instantly share code, notes, and snippets.

@data-doge
Created March 25, 2015 02:53
Show Gist options
  • Save data-doge/242086a39aab490c99ef to your computer and use it in GitHub Desktop.
Save data-doge/242086a39aab490c99ef to your computer and use it in GitHub Desktop.
orange trees 1 ruby
# This is how you define your own custom exception classes
class NoOrangesError < StandardError
end
class OrangeTree
attr_reader :height, :age
MAX_AGE = 13
FRUIT_BEARING_AGE = 4
# Ages the tree one year
def initialize
@age = 0
@height = 0
@oranges = []
end
def age!
@age += 1
@height += rand(0.3..0.5)
rand(100..300).times {@oranges << Orange.new(rand(0.1..0.3))} if @age > FRUIT_BEARING_AGE
end
def dead?
@age >= MAX_AGE
end
# Returns +true+ if there are any oranges on the tree, +false+ otherwise
def any_oranges?
@oranges.length > 0
end
# Returns an Orange if there are any
# Raises a NoOrangesError otherwise
def pick_an_orange!
raise NoOrangesError, "This tree has no oranges" unless self.any_oranges?
# orange-picking logic goes here
@oranges.pop
end
end
class Orange
attr_reader :diameter
# Initializes a new Orange with diameter +diameter+
def initialize(diameter)
@diameter = diameter
end
end
tree = OrangeTree.new
tree.age! until tree.any_oranges?
puts "Tree is #{tree.age} years old and #{tree.height} feet tall"
until tree.dead?
basket = []
# It places the oranges in the basket
# IT PLACES THE ORANGES IN THE BASKET
while tree.any_oranges?
basket << tree.pick_an_orange!
end
# It's up to you to calculate the average diameter for this harvest.
sum = 0
basket.each {|orange| sum += orange.diameter}
avg_diameter = sum/basket.length
# another way of achieving the average diameter:
# avg_diameter = basket.inject(0) {|sum, orange| sum + orange.diameter}/basket.length
puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
puts "Harvest: #{basket.size} oranges with an average diameter of #{avg_diameter} inches"
puts ""
# Age the tree another year
tree.age!
end
puts "Alas, the tree, she is dead!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment