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

finished challenge #69

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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
source 'https://rubygems.org'
gem 'rspec'
gem 'pry'
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.2)
diff-lcs (1.2.5)
method_source (0.9.0)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rspec (3.1.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
Expand All @@ -19,7 +24,8 @@ PLATFORMS
ruby

DEPENDENCIES
pry
rspec

BUNDLED WITH
1.15.3
1.16.1
37 changes: 31 additions & 6 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
require 'pry'

class NoApplesError < StandardError; end

class Tree
attr_#fill_in :height, :age, :apples, :alive
attr_accessor :height, :age, :apples, :alive

def initialize
@height = 1
@age = 0
@apples = []
@alive = true
end

def age!
self.age += 1
unless self.height >= 20
self.height += 1
end
self.age > 8 ? self.add_apples : false
self.dead? ? self.alive = false : self.alive = true
Copy link
Member

@mikegee mikegee May 14, 2018

Choose a reason for hiding this comment

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

Flipping this around is a bit simpler, I think:

self.alive = !self.dead?

end

def add_apples
3.times do self.apples << Apple.new("red", rand(2..4)) end
Copy link
Member

Choose a reason for hiding this comment

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

Rubyists tend to use curly braces for single-line blocks:

3.times { self.apples << Apple.new("red", rand(2..4)) }

end

def any_apples?
self.apples.length > 0
end

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

def dead?
self.age >= 100
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
#what should go here

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

Expand Down Expand Up @@ -61,8 +83,9 @@ def tree_data
diameter_sum += apple.diameter
end

avg_diameter = # It's up to you to calculate the average diameter for this harvest.

# It's up to you to calculate the average diameter for this harvest.
avg_diameter = diameter_sum/basket.length

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
puts "Harvest: #{basket.size} apples with an average diameter of #{avg_diameter} inches"
Expand All @@ -77,3 +100,5 @@ def tree_data

# Uncomment this line to run the script, but BE SURE to comment it before you try to run your tests!
# tree_data

# Tree.new
74 changes: 72 additions & 2 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,80 @@
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end

let(:fuji) {Tree.new}

it 'has a height, age, apples, alive' do
expect(fuji.height).to eq(1)
expect(fuji.age).to eq(0)
expect(fuji.apples).to be_empty
expect(fuji.alive).to be(true)
end

it 'does not have apples when initialized' do
expect(fuji.any_apples?).to eq(false)
end

it 'is not dead when initialized' do
expect(fuji.dead?).to eq(false)
end

it 'ages' do
fuji.age!
expect(fuji.height).to eq(2)
expect(fuji.age).to eq(1)
expect(fuji.apples).to be_empty
expect(fuji.alive).to be(true)
end

it 'stops growing after it is 20 feet tall' do
Copy link
Member

Choose a reason for hiding this comment

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

looks good but keep in mind using context to clean it defs like this and make them more readable.

21.times do fuji.age! end
expect(fuji.height).to eq(20)
end

it 'has apples after it is 8 years old' do
9.times do fuji.age! end
expect(fuji.any_apples?).to be(true)
end

it 'has more than one apple after it is 9 years old' do
10.times do fuji.age! end
expect(fuji.apples.length).to eq(6)
end

it 'dies after it is 100 years old' do
101.times do fuji.age! end
expect(fuji.dead?).to be(true)
expect(fuji.alive).to be(false)
end

end

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

let(:plum) {Fruit.new}

it 'has seeds' do
expect(plum.has_seeds).to be(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

let(:gala) {Apple.new("pink", 4)}

it 'has a color and diameter' do
expect(gala.color).to eq("pink")
expect(gala.diameter).to eq(4)
end

it 'has seeds' do
expect(gala.has_seeds).to be(true)
end
end