From f0baefe92f95ad0fdb151dfd6e2d06b08c9093e2 Mon Sep 17 00:00:00 2001 From: matthew lee Date: Wed, 10 Oct 2018 16:44:50 -0400 Subject: [PATCH 1/3] . --- lib/tree.rb | 32 +++++++++++++++++++------------- spec/tree_spec.rb | 19 +++++++++++++++++-- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..5ef3326 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -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 do @apples.push(Apple.new()) end 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 @@ -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'].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? @@ -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 @@ -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 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 diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..ba79613 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,10 +5,25 @@ 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 end -describe 'Fruit' do +describe Fruit do + it 'should be a Class' do + expect(described_class.is_a? Class).to eq true + end + end -describe 'Apple' do +describe Apple do + it 'apple should be a an instance of Apple and subclass of Fruit' do + apple = Apple.new() + expect(apple).to be_a_kind_of(Fruit) + end end From 71218bef7d88b249f44d8a2edbe73b737b06d7ea Mon Sep 17 00:00:00 2001 From: matthew lee Date: Wed, 10 Oct 2018 21:01:04 -0400 Subject: [PATCH 2/3] . --- lib/tree.rb | 4 ++-- spec/tree_spec.rb | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 5ef3326..c30ac0f 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -16,7 +16,7 @@ def age! end def add_apples - rand(5..15).times do @apples.push(Apple.new()) end + rand(5..15).times { @apples.push(Apple.new } end def any_apples? @@ -43,7 +43,7 @@ class Apple < Fruit attr_reader :color, :diameter def initialize() - @color = ['red'].sample + @color = ['red','yellow', 'green'].sample @diameter = rand(5..10) end end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index ba79613..6bc483e 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -22,8 +22,11 @@ end describe Apple do - it 'apple should be a an instance of Apple and subclass of Fruit' 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_a_kind_of(Fruit) + expect(apple).to be_an_instance_of(Apple) end end From 2ab92199b6946e395ec27ca79a64ec51b3f6c600 Mon Sep 17 00:00:00 2001 From: matthew lee Date: Mon, 5 Nov 2018 12:50:06 -0500 Subject: [PATCH 3/3] missing ) --- lib/tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index c30ac0f..a19baee 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -16,7 +16,7 @@ def age! end def add_apples - rand(5..15).times { @apples.push(Apple.new } + rand(5..15).times { @apples.push(Apple.new) } end def any_apples?