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
Empty file modified Gemfile
100644 → 100755
Empty file.
Empty file modified Gemfile.lock
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
36 changes: 30 additions & 6 deletions lib/tree.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
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 += 1
Copy link
Member

Choose a reason for hiding this comment

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

The attr_reader at the top exposes @age, etc. by calling methods age.

This line is both reading and assigning age if you used attr_accessor, to define the setter methods too, this line could be age += 1.

I prefer depending on the methods as much as possible. Ruby will gladly return nil for a typoed instance variable, but raises an error for a typoed method name.

@height += 1
self.add_apples
if @age == 16
@alive = false
end
end

def add_apples
$i = 0
Copy link
Member

Choose a reason for hiding this comment

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

You may have intended to use a local variable here, but that $ prefix is making this a global instead. I think you can drop the $s in here.

$num = rand(0...6)
possibleColors = ['red', 'green', 'yellow', 'dark red']
until $i > $num
@apples.push Apple.new(possibleColors[rand(0...3)], rand(1...4))
$i += 1
end
end

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

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 +85,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 Down
72 changes: 68 additions & 4 deletions spec/tree_spec.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
require 'rspec'
require 'tree'

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

it 'should add apples' do
appleTree = Tree.new
appleTree.add_apples
expect(appleTree.apples.count()).to be > 0
end

it 'should pick an apple' do
appleTree = Tree.new
appleTree.add_apples
startingApples = appleTree.apples.count()
appleTree.pick_an_apple!
endingApples = appleTree.apples.count()
expect(startingApples).to be > endingApples
end

it 'should age' do
appleTree = Tree.new
appleTree.age!
expect(appleTree.age).to eq 1
expect(appleTree.height).to eq 1
expect(appleTree.apples.count()).to be > 0
end

it 'should reveal alive status' do
appleTree = Tree.new
appleTree.age!
expect(appleTree.dead?).to eq false
end

it 'should reveal dead status' do
appleTree = Tree.new
16.times { appleTree.age! }
expect(appleTree.dead?).to eq true
end

end

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

it 'should have seeds' do
aFruit = Fruit.new
expect(aFruit.has_seeds).to eq true
end
end

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

it 'should have seeds' do
apple = Apple.new("red", 3)
expect(apple.has_seeds).to eq true
end

it 'should have a color' do
apple = Apple.new("red", 3)
expect(apple.color).to be_a_kind_of(String)
expect(apple.color).to eq "red"
end

it 'should have a diameter' do
apple = Apple.new("green", 2)
expect(apple.diameter).to be_a_kind_of(Numeric)
expect(apple.diameter).to eq 2
end
end