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
38 changes: 32 additions & 6 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
class NoApplesError < StandardError; end

class AppleTree
attr_#fill_in :height, :age, :apples, :alive
class Tree
attr_accessor :height, :age, :apples, :alive

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

def age!
@age += 1
if @age > 24 then
die!
else
@height += Random.rand(1.0..2.0).round(2)
add_apples if age > 4
end
end

def die!
@alive = false
end

def add_apples
range = (@height.to_i-3..@height.to_i+3)
Random.rand(range).times do
@apples << Apple.new('red', Random.rand(2.0..4.0))
end
end

def any_apples?
return @apples.size > 0
end

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

def dead?
return !@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

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

Expand Down Expand Up @@ -61,7 +87,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.size.to_f).round(2)

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand Down
82 changes: 78 additions & 4 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,88 @@
require 'rspec'
require 'tree'

describe 'Tree' do
describe Tree do

before :each do
@tree = Tree.new
end

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

it 'should be newly-planted' do
expect(@tree.height).to eq 0
expect(@tree.age).to eq 0
expect(@tree.apples.size).to eq 0
expect(@tree.alive).to be true
end

it 'should be 1 year old' do
@tree.age!
expect(@tree.age).to eq 1
end

it 'should have fruit' do
8.times { @tree.age! }
expect(@tree.any_apples?).to be true
end

it 'should grow more fruit with age' do
5.times { @tree.age! }
3.times do
old_apples = @tree.apples.size
4.times { @tree.age! }
new_apples = @tree.apples.size
expect(new_apples).to be >= old_apples
end
end

it 'should pick apples' do
8.times { @tree.age! }
2.times do
apple = @tree.pick_an_apple!
expect(apple.is_a? Apple).to be true
expect(apple.color).to eq 'red'
end
end

it 'should die' do
26.times { @tree.age! }
expect(@tree.dead?).to be true
end
end

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

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

describe 'Apple' do
describe Apple do

before :each do
@apple = Apple.new('red', 3)
end

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

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

it 'should be red' do
expect(@apple.color).to eq 'red'
end

it 'should be 3 inches in diameter' do
expect(@apple.diameter).to eq 3
end
end