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
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.15.1
31 changes: 24 additions & 7 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
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 = []
@alive = true
end

def age!
@age = age + 1
@height = height + 1
@alive = false if @age > 25
add_apples
end

def add_apples
apple = 12.times { @apples.push Apple.new("Red", Random.rand(1..6)) } #creates 12 Red apples with ramdom diameters each year
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 #to remove it
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

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

Expand Down Expand Up @@ -61,7 +78,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

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand All @@ -76,4 +93,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
73 changes: 69 additions & 4 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,79 @@
require 'rspec'
require 'tree'

describe 'Tree' do
tree = Tree.new

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

context 'when grows one foot' do
it "equals 1" do
tree.age!
expect(tree.height).to eq(1)
end
end

context 'when ages one year' do
it "equals 1" do
expect(tree.age).to eq(1)
end
end

context 'when has no fruit' do
it "equals true" do
expect(tree.any_apples?).to eq true
end
end

context 'when has apples after 3 years' do
it "equals true" do
tree.age!
expect(tree.any_apples?).to be true
end
end

context 'when dies after 25 years' do
it "equals false" do
(1..13).each{ |idx| tree.age!}
Copy link
Member

Choose a reason for hiding this comment

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

This works. You could also use .times which would make this cleaner.
https://ruby-doc.org/core-2.2.2/Integer.html#method-i-times

expect(tree.dead?).to be false
end
end
end

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

describe 'Apple' do
apple = Apple.new("Red", 30)
Copy link
Member

Choose a reason for hiding this comment

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

For things like this, we use let. Refactor any variables used here using let instead.
http://www.betterspecs.org/#let


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

it 'is a Fruit' do
expect(Apple < Fruit).to be true # expects to be inheriting from apple if it is a fruit
Copy link
Member

Choose a reason for hiding this comment

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

I like this. There's a couple of other ways to do this as well.
https://stackoverflow.com/questions/23027236/how-to-test-ruby-inheritance-with-rspec

The documentation spells out your method here.
https://ruby-doc.org/core-2.3.0/Module.html#method-i-3C

end

context 'when sets a color' do
it "equals red" do
expect(apple.color).to eq("Red")
end
end

context 'when sets a diameter' do
it "equals 30" do
expect(apple.diameter).to eq(30)
end
end

context 'when calls super()' do
it "equals true" do
expect(apple.has_seeds).to eq true
end
end
end