From f4080c0fb044b59dcc0c0553abccd96cb5b77f8e Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Fri, 19 Jan 2018 15:26:38 -0500 Subject: [PATCH 1/5] Describe an apple tree that ages, grows, etc --- Gemfile.lock | 4 ++++ lib/tree.rb | 37 ++++++++++++++++++++++++++++------- spec/tree_spec.rb | 50 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..6025109 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: diff-lcs (1.2.5) rspec (3.1.0) @@ -19,3 +20,6 @@ PLATFORMS DEPENDENCIES rspec + +BUNDLED WITH + 1.16.1 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..42dd8f5 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,61 @@ class NoApplesError < StandardError; end + class AppleTree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive def initialize + @height = 0 + @age = 0 + @apples = [] + @alive = true + @colors = ['red', 'green', 'yellow'] end def age! + @age += 1 + @height += rand(1..(2*@age)) + self.add_apples end def add_apples + rand(1..(4*@age)).times do + @apples << Apple.new(@colors[rand(0..2)], rand(1..5)) + end end def any_apples? + @apples.length > 0 ? true : false end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.slice!(rand(0...@apples.length)) end def dead? + if @height > 60 || @age > 13 + true + else + false + end end end class Fruit + attr_reader :has_seeds + def initialize - has_seeds = true + @has_seeds = true end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -41,7 +64,7 @@ def initialize(color, diameter) # it should calculate the diameter of the apples in the basket def tree_data - tree = Tree.new + tree = AppleTree.new tree.age! until tree.any_apples? @@ -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 puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +99,4 @@ def tree_data end # Uncomment this line to run the script, but BE SURE to comment it before you try to run your tests! -# tree_data +tree_data diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 99c9184..8dd1f65 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,54 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true +describe AppleTree do + let(:tree) { AppleTree.new } + + it { expect(tree.class).to be AppleTree } + + it 'will age' do + tree.age! + expect(tree.age).to eq 1 + end + + it 'can add apples' do + tree.age = 4 + tree.add_apples + expect(tree.any_apples?).to be true + end + + context 'when picking apples' do + + it 'raises an error for no apples' do + expect { tree.pick_an_apple! }.to raise_error(NoApplesError, "This tree has no apples") + end + + it 'removes an apple when present' do + tree.age! + remaining_apples = tree.apples.length - 1 + tree.pick_an_apple! + expect(tree.apples.length).to eq remaining_apples + end + end end -describe 'Fruit' do +describe Fruit do + let(:fruit) { Fruit.new } + + it { expect(fruit.class).to be Fruit } + + it { expect(fruit.has_seeds).to be true } end -describe 'Apple' do +describe Apple do + let(:apple) { Apple.new('red', 5) } + + it "should have a color" do + expect(apple.color).to eq 'red' + end + + it 'should have a diameter' do + expect(apple.diameter).to eq 5 + end end From f27bc1ce791d8b22c4470f2d34f8ae542f40b043 Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Sun, 21 Jan 2018 12:29:13 -0500 Subject: [PATCH 2/5] Move Apple property assignment to Apple class --- lib/tree.rb | 20 ++++++++------------ spec/tree_spec.rb | 7 ++++--- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 42dd8f5..b8fc94e 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -9,7 +9,6 @@ def initialize @age = 0 @apples = [] @alive = true - @colors = ['red', 'green', 'yellow'] end def age! @@ -20,12 +19,12 @@ def age! def add_apples rand(1..(4*@age)).times do - @apples << Apple.new(@colors[rand(0..2)], rand(1..5)) + @apples << Apple.new end end def any_apples? - @apples.length > 0 ? true : false + @apples.empty? ? false : true end def pick_an_apple! @@ -34,11 +33,7 @@ def pick_an_apple! end def dead? - if @height > 60 || @age > 13 - true - else - false - end + @height > 60 || @age > 13 end end @@ -53,9 +48,10 @@ def initialize class Apple < Fruit attr_reader :color, :diameter - def initialize(color, diameter) - @color = color - @diameter = diameter + def initialize + colors = ['red', 'green', 'yellow'] + @color = colors.sample + @diameter = rand(1..5).to_f end end @@ -84,7 +80,7 @@ def tree_data diameter_sum += apple.diameter end - avg_diameter = diameter_sum/basket.length + avg_diameter = (diameter_sum/basket.length).round(2) puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 8dd1f65..e38f304 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -42,13 +42,14 @@ end describe Apple do - let(:apple) { Apple.new('red', 5) } + let(:apple) { Apple.new } it "should have a color" do - expect(apple.color).to eq 'red' + colors = ['red', 'green', 'yellow'] + expect(colors).to include apple.color end it 'should have a diameter' do - expect(apple.diameter).to eq 5 + expect(apple.diameter).to be_between(1,5).inclusive end end From 50c05beef3c29eb6a4412e07afe8646252edc599 Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Sun, 21 Jan 2018 12:34:37 -0500 Subject: [PATCH 3/5] Update Tree aging spec --- spec/tree_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index e38f304..a9c748a 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -7,8 +7,10 @@ it { expect(tree.class).to be AppleTree } it 'will age' do + tree.age = rand(1..10) + previous_tree_age = tree.age tree.age! - expect(tree.age).to eq 1 + expect(tree.age).to eq (previous_tree_age + 1) end it 'can add apples' do From d7345292f893064ee45303f759ee1707c5067134 Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Sun, 21 Jan 2018 12:48:55 -0500 Subject: [PATCH 4/5] Improve clarity of spec for picking an apple --- spec/tree_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index a9c748a..549033d 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -26,7 +26,8 @@ end it 'removes an apple when present' do - tree.age! + tree.age = rand(1..12) + tree.add_apples remaining_apples = tree.apples.length - 1 tree.pick_an_apple! expect(tree.apples.length).to eq remaining_apples From f6ee39b098d28cedf3ddd19a9d81935fc74fe9a2 Mon Sep 17 00:00:00 2001 From: Rupert Saxton Date: Sun, 21 Jan 2018 12:50:04 -0500 Subject: [PATCH 5/5] Improve nomenclature in test for picking apples --- spec/tree_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 549033d..bba07ee 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -28,9 +28,9 @@ it 'removes an apple when present' do tree.age = rand(1..12) tree.add_apples - remaining_apples = tree.apples.length - 1 + remaining_apples_count = tree.apples.length - 1 tree.pick_an_apple! - expect(tree.apples.length).to eq remaining_apples + expect(tree.apples.length).to eq remaining_apples_count end end