-
Notifications
You must be signed in to change notification settings - Fork 74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
source 'https://rubygems.org' | ||
gem 'rspec' | ||
gem 'pry' |
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 | ||
end | ||
|
||
def add_apples | ||
3.times do self.apples << Apple.new("red", rand(2..4)) end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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" | ||
|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good but keep in mind using context to clean |
||
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 |
There was a problem hiding this comment.
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: