Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe an apple tree that ages, grows, etc #59

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

RupertSaxton
Copy link

No description provided.

@RupertSaxton
Copy link
Author

Sorry that I didn't make it into smaller commits

lib/tree.rb Outdated
@age = 0
@apples = []
@alive = true
@colors = ['red', 'green', 'yellow']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should apple colors be a property of the tree, or of the apple? I guess you could make a case either way. But given what you're doing with it here, it might make sense to move the random color- and size-generation into Apple#initialize

lib/tree.rb Outdated
end

def add_apples
rand(1..(4*@age)).times do
@apples << Apple.new(@colors[rand(0..2)], rand(1..5))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the ruby sample method

lib/tree.rb Outdated
end

def any_apples?
@apples.length > 0 ? true : false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is empty?, and at least when you have ActiveSupport available, present?.

lib/tree.rb Outdated
true
else
false
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be @height > 60 || @age > 13, right?
But what I'm really wondering is why it isn't just !@alive

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the simplicity of @height > 60 || @age > 13. Since @alive isn't used or necessary I think I'll just remove it

lib/tree.rb Outdated
@@ -61,7 +84,7 @@ def tree_data
diameter_sum += apple.diameter
end

avg_diameter = # It's up to you to calculate the average diameter for this harvest.
avg_diameter = diameter_sum/basket.length
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it will always return an integer. Is that what is intended?

it 'will age' do
tree.age!
expect(tree.age).to eq 1
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One option here is to set the age to a random number, then make it age, then check that the new age is one higher than the old one.


it 'raises an error for no apples' do
expect { tree.pick_an_apple! }.to raise_error(NoApplesError, "This tree has no apples")
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test.

end

it 'removes an apple when present' do
tree.age!
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_apples might be clearer setup here

tree.age!
remaining_apples = tree.apples.length - 1
tree.pick_an_apple!
expect(tree.apples.length).to eq remaining_apples
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remaining_apples makes it sounds like the value is an array of apples. Maybe remaining_apple_count or similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants