Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created February 26, 2013 16:48
Show Gist options
  • Save burtlo/5040018 to your computer and use it in GitHub Desktop.
Save burtlo/5040018 to your computer and use it in GitHub Desktop.
Delaying size of the apple until after initialization
module Fruit
class Apple
attr_reader :variety, :created_at
def initialize(variety = default_variety)
@variety = variety
@created_at = Time.now
end
def initial_size
rand(5..9)
end
def slices
@slices ||= [1] * initial_size
@slices
end
def slice(number=1)
slices.pop(number).count
end
def remaining_slices
slices.count
end
def default_variety
"Granny Smith"
end
def age
Time.now - created_at
end
def ripe?
age > maturity_time
end
def unripe?
!ripe?
end
def maturity_time
10.0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment