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

Fixed the tree. :-0 #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 29 additions & 6 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,61 @@
class NoApplesError < StandardError; end

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

def initialize
@age = 0
@height = 0
@apples = []
@alive = true
end

def age!
@age += 1
@height += 2
add_apples
dead?
Copy link
Member

@mikegee mikegee May 14, 2018

Choose a reason for hiding this comment

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

I think its a bit less surprising if this method can kill the tree, rather than when you take its pulse (dead?).

end

def add_apples
if (@age > 2 && @age < 13) && !dead?
(5 * @age).times { @apples.push(Apple.new('green', rand(1..4))) }
end
@apples.count
end

def any_apples?
@apples.count > 0
end

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

def dead?
if @age == 13
@alive = false
true
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)
super()
@color = color
@diameter = diameter
end
end

Expand Down Expand Up @@ -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.count

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand All @@ -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
55 changes: 55 additions & 0 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,65 @@
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end

let(:t) { Tree.new }

it 'is initaialized with expected values for attributes' do
expect(t.age).to eq 0
expect(t.height).to eq 0
expect(t.apples).to eq []
expect(t.alive).to be(true)
end

it 'ages' do
t.age!
expect(t.age).to eq 1
expect(t.apples).to eq []
end

it 'does not produce apples for first 2 years' do
2.times { t.age! }
expect(t.apples).to eq []
end

it 'produces 5 apples when aged 3 years' do
3.times { t.age! }
expect(t.apples.count).to eq 15
end

it 'can be picked for apples' do
3.times { t.age! }
t.pick_an_apple!
expect(t.apples).not_to be_empty
end

it 'dies when it ages beyond 12 years' do
13.times { t.age! }
expect(t.alive).to be(false)
end
end

describe 'Fruit' do
it "should have seeds" do
f = Fruit.new
expect(f.has_seeds).to be(true)
end
end

describe 'Apple' do

let(:a) { Apple.new("red", 2) }

it "has super class' attributes" do
expect(a.has_seeds).to be(true)
end

it "is red" do
expect(a.color).to eq ("red")
end

it "has a diameter" do
expect(a.diameter).to eq 2
end

end