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

. #75

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

. #75

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
class NoApplesError < StandardError; end

class Tree
attr_#fill_in :height, :age, :apples, :alive
attr_reader :height, :age, :apples, :alive

def initialize
def initialize(height, age, apples, alive)
@height = height
@age = age
@apples = apples
@alive = alive
end

def age!
add_apples unless @age < 3
@age += 1
end

def add_apples
rand(5..15).times { @apples.push(Apple.new) }
end

def any_apples?
@apples.size > 0
end

def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
@apples.shift()
end

def dead?
age > 10
end
end

Expand All @@ -29,19 +39,17 @@ def initialize
end
end

class Apple <
attr_reader #what should go here
class Apple < Fruit
attr_reader :color, :diameter

def initialize(color, diameter)
def initialize()
@color = ['red','yellow', 'green'].sample
@diameter = rand(5..10)
end
end

#THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE
# avg_diameter (line 58) will raise an error.
# it should calculate the diameter of the apples in the basket

def tree_data
tree = Tree.new
tree = Tree.new(5, 1, [], true)

tree.age! until tree.any_apples?

Expand All @@ -50,7 +58,6 @@ def tree_data
until tree.dead?
basket = []

# It places the apple in the basket
while tree.any_apples?
basket << tree.pick_an_apple!
end
Expand All @@ -61,14 +68,13 @@ 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.size
Copy link

Choose a reason for hiding this comment

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

Looks like this is doing integer division. Is that what is intended?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that's what I meant to do. Am I doing it incorrectly?

Copy link

@sjreich sjreich Oct 11, 2018

Choose a reason for hiding this comment

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

Suppose you a had basket with two apples in it, with diameters of 7 and 8, respectively. What would you expect the average diameter to be? What does this code say the average diameter is?


puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
puts "Harvest: #{basket.size} apples with an average diameter of #{avg_diameter} inches"
puts ""

# Ages the tree another year
tree.age!
end

Expand Down
22 changes: 20 additions & 2 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end
it 'should create an instance of Tree with correct attributes' do
tree = Tree.new(5, 1, 0, true)
expect(tree.height).to eq(5);
expect(tree.age).to eq(1);
expect(tree.apples).to eq(0);
expect(tree.alive).to eq(true);
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

describe 'Fruit' do
describe Fruit do
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end
Copy link

Choose a reason for hiding this comment

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

This kind of test is quite possibly not worth having in a project. If Fruit isn't a class, then all kinds of stuff should be blowing up in your tests.


end

describe 'Apple' do
describe Apple do
it 'Apple should be a subclass of Fruit' do
expect(Apple).to be < Fruit
end
it 'apple should be a an instance of Apple' do
apple = Apple.new()
expect(apple).to be_an_instance_of(Apple)
end
Copy link

Choose a reason for hiding this comment

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

If you find yourself putting 'and' in the description of your test, then you should consider writing two tests instead.

Copy link
Author

Choose a reason for hiding this comment

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

I've changed it to be two tests, one for Apple < Fruit, and one for apple to be_an_instance_of(Apple)

end