Skip to content
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
34 changes: 29 additions & 5 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
class NoApplesError < StandardError; end

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

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

def age!
if @age.between?(0,5)
@age += 1
@height += rand(1..3)
elsif @age.between?(5,10)
@age+=1
@height += rand(1..3)
add_apples
else
@alive = false
end
end

def add_apples
rand(1..50).times do
@apples << Apple.new("red", rand(1..3))
end
end

def any_apples?
@apples.length > 0
end

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

def dead?
!@alive
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#what should go here

def initialize(color, diameter)
super()
@color = color
@diameter = diameter
end
end

Expand Down Expand Up @@ -61,7 +85,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# It's up to you to calculate the average diameter for this harvest.

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand Down
59 changes: 58 additions & 1 deletion spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,70 @@
require_relative '../lib/tree'

describe Tree do
let (:tree) {Tree.new}

it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
expect(Tree.is_a? Class).to eq true
end

it 'should initalize empty' do
expect(tree.apples).to eq []
expect(tree.alive).to eq true
expect(tree.height).to eq 0
expect(tree.age).to eq 0
end

it 'should grow when it ages' do
tree.age!
expect(tree.height > 0).to eq true
expect(tree.age).to eq 1
end

it 'should gorw apples' do
7.times {tree.age!}
expect(tree.any_apples?).to eq true
end

it 'should be able to pick an apple' do
7.times {tree.age!}
expect(tree.pick_an_apple!).to be_a Apple
end

it 'should die of old age' do
12.times {tree.age!}
expect(tree.alive).to eq false
end
end

describe 'Fruit' do
let(:fruit) {Fruit.new}


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

it 'should have seeds' do
expect(fruit.has_seeds).to eq true
end
end

describe 'Apple' do
let(:apple) {Apple.new('Green', 2)}

it 'should be a Class' do
expect(Apple.is_a? Class).to eq true
end

it 'should have seeds' do
expect(apple.has_seeds).to eq true
end

it 'should have color' do
expect(apple.color).to eq 'Green'
end

it 'should have a diameter' do
expect(apple.diameter).to eq 2
end
end