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
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (3.1.0)
Expand All @@ -19,3 +20,6 @@ PLATFORMS

DEPENDENCIES
rspec

BUNDLED WITH
1.13.7
34 changes: 27 additions & 7 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
class NoApplesError < StandardError; end

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

def initialize
@height=0
@age=0
@apples=Array.new
@alive=true
end

def age!
if @age>5
@alive=false
@apples.clear
else
@age+=1
self.add_apples
@height+=1
end
end

def add_apples
new_apple= Apple.new("red",2)
(1+Random.new.rand(5)).times{@apples << new_apple}
end

def any_apples?
@apples.count!=0
Copy link
Member

Choose a reason for hiding this comment

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

Would you believe Arrays have an any? method?

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
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, :has_seeds

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

Expand Down Expand Up @@ -61,7 +81,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 +96,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
90 changes: 85 additions & 5 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,94 @@
require 'rspec'
require 'tree'

describe 'Tree' do
it 'should be a Class' do
expect(described_class.is_a? 'Class').to be_true
#run with rspec spec/tree_spec
shared_examples "class_check" do |tree_class|
it 'is a class' do
expect(tree_class.is_a? Class).to be true
end
end

describe 'Fruit' do
describe Tree do
before(:each) do
@tree=Tree.new
end

include_examples "class_check", described_class

it 'should be alive when born' do
expect(@tree.dead?).to be false
end

it 'should have no apples at the start' do
expect(@tree.any_apples?).to be false
end

it 'should have no height when born' do
expect(@tree.height==0)
end

it 'should raise an error when popping no apples' do
expect(@tree.any_apples?).to be false
expect{@tree.pick_an_apple!}.to raise_error NoApplesError
end

it 'should increase apples as it ages' do
apple_count_before = @tree.apples.count
@tree.age!
apple_count_after = @tree.apples.count
expect(apple_count_before<apple_count_after)
end

it 'should grow in height as it ages' do
height_before = @tree.height
@tree.age!
height_after = @tree.height
expect(height_before < height_after)
end

it 'should die after five year of age' do
7.times {@tree.age!}
expect(@tree.dead?).to be true
end

it 'should have no apples after five years of age' do
7.times{@tree.age!}
expect(@tree.any_apples?).to be false
end

it 'should have the same height as before it dies' do
7.times{@tree.age!}
before_height=@tree.height
@tree.age!
after_height=@tree.height
expect(before_height==after_height)
end

end

describe Fruit do
include_examples "class_check", described_class
end

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

include_examples "class_check", described_class

it 'should be an inheriting class' do
expect(described_class < Fruit). to be true
end

it 'should be red' do
expect(@apple.has_seeds).to be true
end
it 'should have diameter 2' do
expect(@apple.diameter==2)
end
it 'should be red' do
expect(@apple.color=="red")
end

end