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

Hari's solution #60

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

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

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

def age!
@age += 1 if @alive
@height += rand(0..2) if @alive
add_apples if @alive and @age > 5
@alive = false if @age > 30 or @height > 30
end

def add_apples
rand(2..30).times{ |x| @apples << Apple.new('green', rand(1..4))}
end

def any_apples?
not @apples.empty?
end

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

def dead?
not @alive
end

end

class Fruit
Expand All @@ -29,10 +43,12 @@ def initialize
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

Expand Down Expand Up @@ -61,7 +77,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
57 changes: 56 additions & 1 deletion spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,68 @@
require 'tree'

describe 'Tree' do

let(:tree) { Tree.new }

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

it 'should have no apples initially' do
expect(tree.any_apples?).to eq false
end

it 'should be able to add/grow apples' do
tree.add_apples
expect(tree.any_apples?).to eq true
end

it 'dies after living for 30 years' do
32.times { |x| tree.age! }
expect(tree.dead?).to eq true
end

it 'dies when it is 30 feets height' do
tree.height = 31
tree.age!
expect(tree.dead?).to eq true
end

it 'should age' do
tree.age!
expect(tree.age).to eq 1
end

it 'should be able to pick an apple' do
6.times { |x| tree.age! }
expect(tree.pick_an_apple!.is_a? Apple).to eq true
end

it 'should raise problem when no apples' do
expect{tree.pick_an_apple!}.to raise_error NoApplesError
end

end

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

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

it 'should be a Fruit Class' do
expect(Apple.superclass).to eq Fruit
end

it 'has color and diameter' do
apple = Apple.new('green', 2)
expect(apple.color).to eq 'green'
expect(apple.diameter).to eq 2
end

end