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

Jack Chester Solutions #53

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
36 changes: 29 additions & 7 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
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 = 0
end

def age!
@age += 1
@height += 1
@alive = false if @age > 15
add_apples if @age % 3 == 0
end

def add_apples
(1..10).each do |idx|
Copy link
Member

Choose a reason for hiding this comment

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

Could you also do something like this...

10.times { @apples.push Apple.new("Red", rand(4..6)) }

I'd assume the diameter would have less of a variance :)

https://ruby-doc.org/core-2.2.2/Integer.html#method-i-times

apple = Apple.new("Red", idx)
@apples.push(apple)
end
end

def any_apples?
return @apples.length > 0
end

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

def dead?
return !@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 :diameter
attr_reader :color

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

super()
end
end

Expand Down Expand Up @@ -61,7 +83,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 = if basket.size > 0; diameter_sum / basket.size else 0 end # It's up to you to calculate the average diameter for this harvest.

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand All @@ -76,4 +98,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
56 changes: 54 additions & 2 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
require 'rspec'
require 'tree'

testTree = Tree.new

describe 'Tree' do
Copy link
Member

Choose a reason for hiding this comment

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

describe Tree do will work.

Then you can do stuff like described_class.new.age! or even subject { described_class.new }

Copy link
Member

Choose a reason for hiding this comment

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

Also check this out... www.betterspecs.org/#let

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

it 'grows one foot' do
testTree.age!
expect(testTree.height).to eq(1)
end

it 'ages one year' do
expect(testTree.age).to eq(1)
Copy link
Member

Choose a reason for hiding this comment

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

2 spaces

end

it 'has no fruit' do
expect(testTree.any_apples?).to be false
end


it 'has apples after 3 years' do
testTree.age!
testTree.age!
expect(testTree.any_apples?).to be true
end

it 'dies after 16 years' do
(1..13).each{ |idx| testTree.age!}
expect(testTree.dead?).to be true
end
end

describe 'Fruit' do
it 'is a Class' do
expect(Fruit.is_a? Class).to be true
Copy link
Member

Choose a reason for hiding this comment

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

2 spaces

end
end

testApple = Apple.new("Red", 30)

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

it 'is a Fruit' do
expect(Apple < Fruit).to be true
end


it 'sets a color' do
expect(testApple.color).to eq("Red")
end

it 'sets a diameter' do
expect(testApple.diameter).to eq(30)
end

it 'calls super()' do
expect(testApple.has_seeds).to be true
end
end