Created
February 26, 2013 16:48
-
-
Save burtlo/5040018 to your computer and use it in GitHub Desktop.
Delaying size of the apple until after initialization
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
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