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

Drew's solution #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
35 changes: 29 additions & 6 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
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 > 50
@alive=false
return
end

@height += 1
self.add_apples
end

def add_apples
num_apples = rand(1..5) * @age
i=0
while i < num_apples
Copy link
Member

Choose a reason for hiding this comment

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

Check out the times method to simplify this.

@apples.push(Apple.new('red', rand(1..3)))
i += 1
end
end

def any_apples?
@apples.size > 0
end

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

def dead?
not @alive
end
end

Expand All @@ -29,10 +50,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 +84,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.size > 0 ? diameter_sum / basket.size : 0

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

describe 'Tree' do
it 'should be a Class' do
expect(described_class.is_a? 'Class').to be_true
let(:tree) { Tree.new }

it 'should be a Tree' do
expect(tree.class).to be Tree
end

it 'should age' do
current_age = tree.age
tree.age!
new_age = tree.age
expect(new_age == current_age + 1).to be true
end

it 'should grow apples' do
current_apples = tree.apples.size
tree.add_apples
new_apples = tree.apples.size
expect(current_apples < new_apples).to be true
end

it 'should report if the tree has any apples' do
tree.add_apples
expect(tree.any_apples?).to be true
end

it 'should be able to have apples picked' do
i=0
while i<10
tree.add_apples
i += 1
end
current_apples = tree.apples.size
tree.pick_an_apple!
new_apples = tree.apples.size
expect(new_apples == current_apples - 1).to be true
end

it 'should die after 50 years' do
i=0
while i<51
tree.age!
i +=1
end
expect(tree.dead?).to be true
end
end

describe 'Fruit' do
let(:fruit){Fruit.new}

it 'should be a Fruit' do
expect(fruit.class).to be Fruit
end
end

describe 'Apple' do
let(:apple){Apple.new('red', 2)}

it 'should be an Apple' do
expect(apple.class).to be Apple
end

it 'should be red' do
expect(apple.color == 'red').to be true
end

it 'should have diameter 2' do
expect(apple.diameter == 2).to be true
end
end