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

Justin Holm solution #48

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

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

def initialize
class Tree
attr_reader :height, :age, :apples, :alive

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

def age!
# Age our tree by one year
@age += 1

# Grow our tree a rand height between 1-3 feet per year
@height += rand(1..3)

# Wait for our tree to age until 5, grow some fruit, then steal it
if @age >= 5
rand(100..300).times{ add_apples }
end

# Our poor tree dies exactly on its 30th birthday, assuming its fruit has been picked
if @age >= 30
@alive = false
end
end

def add_apples
# Push a new red apple. Choose a random diameter size between 5-10 inch
@apples.push Apple.new('red', rand(4..10))
end

def any_apples?
@apples.count > 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
end

Choose a reason for hiding this comment

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

Got some trailing white space here. If you're a vim user, I would add set list to your vimrc file so it highlights unwanted white space.

Here's some documentation about it.

Copy link
Author

Choose a reason for hiding this comment

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

Ah, good catch. Thanks!


class Apple <
attr_reader #what should go here
class Apple < Fruit
attr_reader :color, :diameter

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

# Call our Fruit initialize method
super()
end
end

Expand Down Expand Up @@ -61,7 +92,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 = basket.count > 0 ? diameter_sum / basket.count : 0

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand All @@ -76,4 +107,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
82 changes: 81 additions & 1 deletion spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,93 @@
require 'tree'

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? Tree).to eq true
end

it 'should have no (0) age initially' do
expect(@tree.age).to eq 0
end

it 'should have no (0) height initially' do
expect(@tree.height).to eq 0
end

it 'should have no apples initially' do
expect(@tree.apples.count).to eq 0
end

it 'should be alive' do
expect(@tree.alive).to eq true
end

it 'should increase age by 1 when age is called' do
@tree.age!
expect(@tree.age).to eq 1
end

it 'should increase height every year after its first 5 years' do
@tree.age!
expect(@tree.height).not_to eq 0
end

it 'should have apples after the first 5 years' do
5.times{ @tree.age! }
expect(@tree.apples.count).not_to eq 0
end

it 'should die after 30 years' do
30.times{ @tree.age! }
expect(@tree.alive).to eq false
end

it 'should be able to pick an apple' do
basket = []
while @tree.apples.count == 0
@tree.age!
end

basket << @tree.pick_an_apple!
expect(basket.count).to eq 1
end

it 'should decrease the total amount of apples when they are picked' do
basket = []
while @tree.apples.count == 0
@tree.age!
end

num_before = @tree.apples.count
basket << @tree.pick_an_apple!
expect(@tree.apples.count).not_to eq num_before
end
end

describe 'Fruit' do
it 'should be a class' do
@fruit = Fruit.new
expect(@fruit.is_a? Fruit).to eq true
end
end

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

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

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

it 'should have a diameter of 5' do
expect(@apple.diameter).to eq 5
end
end