-
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
Initial add #54
base: master
Are you sure you want to change the base?
Initial add #54
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,38 +1,77 @@ | ||
class NoApplesError < StandardError; end | ||
|
||
|
||
|
||
class AppleTree | ||
attr_#fill_in :height, :age, :apples, :alive | ||
attr_reader :height, :age, :apples, :alive | ||
|
||
def initialize | ||
def initialize(height, age, apples, alive) | ||
@height = height | ||
@age = age | ||
@apples = apples | ||
@alive = alive | ||
end | ||
|
||
def age! | ||
@age = @age + 1 | ||
@height = @height + Random.new.rand(0.5..2.5) | ||
self.add_apples | ||
end | ||
|
||
def add_apples | ||
rn = Random.new | ||
numApples = rn.rand(1..10) | ||
diameter = rn.rand(2.0..4.0) | ||
|
||
for i in 0..numApples | ||
apples.push(Apple.new("Red", diameter)) | ||
end | ||
end | ||
|
||
def any_apples? | ||
if(@apples == nil || @apples.count <= 0) | ||
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. why might we want to use the getter here instead and anywhere else we can in this class? |
||
return false | ||
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. no need for explicit returns here. |
||
end | ||
return true; | ||
end | ||
|
||
def pick_an_apple! | ||
raise NoApplesError, "This tree has no apples" unless self.any_apples? | ||
if(any_apples?) | ||
@apples.pop | ||
end | ||
# raise NoApplesError, "This tree has no apples" unless self.any_apples? | ||
end | ||
|
||
def dead? | ||
if(@age > 50) | ||
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. You could also write this... def dead?
return true if age > 50
false
end |
||
return true | ||
end | ||
return false | ||
end | ||
end | ||
|
||
class Tree < AppleTree | ||
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. Slight nitpick. I might rename this with AppleTree inheriting from Tree. |
||
|
||
def initialize | ||
super(0,0,[],true) | ||
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) | ||
super() | ||
@color = color | ||
@diameter = diameter | ||
end | ||
end | ||
|
||
|
@@ -61,7 +100,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 ? 0 : diameter_sum / basket.count # It's up to you to calculate the average diameter for this harvest. | ||
|
||
puts "Year #{tree.age} Report" | ||
puts "Tree height: #{tree.height} feet" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,110 @@ | ||
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 | ||
end | ||
|
||
describe 'Fruit' do | ||
describe AppleTree 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. Take a look at contexts. I would use those to refactor my tests into a story with a clear happy path and not so happy path. |
||
it 'should be a Class' do | ||
expect(described_class.is_a? Class).to eq true | ||
end | ||
|
||
it 'should set color' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
expect(appleTree.height).to eq 1 | ||
end | ||
|
||
it 'should set age' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
expect(appleTree.age).to eq 1 | ||
end | ||
|
||
it 'should set apples' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
expect(appleTree.apples.count).to eq 2 | ||
end | ||
|
||
it 'should set alive' do | ||
apple = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
expect(apple.alive).to eq true | ||
end | ||
|
||
it 'age! adds one' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
appleTree.age! | ||
expect(appleTree.age).to eq 2 | ||
end | ||
|
||
it 'age! adds height' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
appleTree.age! | ||
expect(appleTree.age).to be > 1 | ||
end | ||
|
||
it 'any_apples returns true if greater than zero' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
expect(appleTree.any_apples?).to eq true | ||
end | ||
|
||
it 'any_apples returns false if nil' do | ||
appleTree = AppleTree.new(1, 1, nil, true) | ||
expect(appleTree.any_apples?).to eq false | ||
end | ||
|
||
it 'any_apples returns false if zero' do | ||
appleTree = AppleTree.new(1, 1, [], true) | ||
expect(appleTree.any_apples?).to eq false | ||
end | ||
|
||
it 'dead? returns true if greater than 50' do | ||
appleTree = AppleTree.new(1, 55, [], true) | ||
expect(appleTree.dead?).to eq true | ||
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. rspec also has dynamic predicate matchers which are pretty cool. https://relishapp.com/rspec/rspec-expectations/v/3-6/docs/built-in-matchers/predicate-matchers |
||
end | ||
|
||
it 'add apples adds apples' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
appleTree.add_apples | ||
expect(appleTree.apples.count).to be > 2 | ||
end | ||
|
||
it 'pick apples removes one apple' do | ||
appleTree = AppleTree.new(1, 1, [Apple.new("Red", 2.5), Apple.new("Red", 3)], true) | ||
appleTree.pick_an_apple! | ||
expect(appleTree.apples.count).to eq 1 | ||
end | ||
end | ||
|
||
describe Fruit do | ||
it 'should be a Class' do | ||
expect(described_class.is_a? Class).to eq true | ||
end | ||
|
||
it 'should have seeds' do | ||
fruit = Fruit.new | ||
expect(fruit.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 set color' do | ||
apple = Apple.new("Red", 2.5) | ||
expect(apple.color).to eq "Red" | ||
end | ||
|
||
it 'should set diamter' do | ||
apple = Apple.new("Red", 2.5) | ||
expect(apple.diameter).to eq 2.5 | ||
end | ||
|
||
it 'should have seeds' do | ||
apple = Apple.new("Red", 2.5) | ||
expect(apple.has_seeds).to eq 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.
You don't have to initialize a new generator to use rand. It's an alias for Random::DEFAULT.rand so you can get away with just writing rand(1..10) here.