Skip to content

Instantly share code, notes, and snippets.

@TalkativeTree
Created June 7, 2013 20:55
Show Gist options
  • Save TalkativeTree/5732367 to your computer and use it in GitHub Desktop.
Save TalkativeTree/5732367 to your computer and use it in GitHub Desktop.
FruitFarm_ruby
class Basket
attr_reader :contents
def initialize
@contents = []
end
def collect(object)
@contents << object
end
end
require_relative 'grove'
require_relative 'tree_and_fruit'
require_relative 'basket'
class Farmer
def initialize
@basket = Basket.new
end
def buy_farm(klass)
@farm = klass.new
end
def plant(klass, seeds)
seeds.times {@farm.crop << klass.new}
end
def count_crop
@farm.crop.count
end
def tend_crop
# p "tending mah crops"
@farm.crop.each{|plant| plant.age!}
end
def unload
@contents.shift(@contents.length)
end
def collect_harvest
# /Users/benjamin/Object Orient Lessons/basket.rb:12:in `collect': undefined method `<<' for nil:NilClass (NoMethodError)
# from farmer_controller.rb:33:in `collect_harvest'
# from farmer_controller.rb:47:in `<main>'
# p "your farm"
# p @farm
# p "what you can do on your farm"
# p @farm.methods
# p "your farm's crop"
# p @farm.crop.first
# p "And does crop work for a normal tree?"
# p (AppleTree.new).crop
# @basket.collect(@farm.crop)
@farm.crop.each { |harvest| @basket.collect(harvest) }
end
end
ben = Farmer.new
ben.buy_farm(Grove)
ben.plant(OrangeTree, 1)
p ben.count_crop == 1
ben.plant(AppleTree, 1)
p ben.count_crop == 2
ben.plant(PearTree, 1)
p ben.count_crop == 3
10.times { ben.tend_crop }
harvest = ben.collect_harvest
harvest.each{ |type| puts "You harvested #{type.fruit.length.to_s + ' ' + type.fruit.first.name}." }
class Fruit
attr_reader :taste, :name
attr_accessor :status
def initialize
@taste = "delicious"
@status = "uneaten"
@name = "fruit"
post_initialize
end
def post_initialize
end
def rot
@status = "spoiled"
end
end
class Apple < Fruit
attr_reader name
def post_initialize
@name = "apple"
end
end
class Orange < Fruit
attr_reader name
def initialize
super
@name = "orange"
end
end
class Pear < Fruit
attr_reader name
def post_initialize
@name = "pear"
end
end
require_relative 'fruit'
module TreeErrors
def tree_alive_error
raise "the tree is still alive, you'll have to chop it down to count it's rings"
end
end
class Tree
include TreeErrors
STAGES = ["seed", "sapling", "teen", "mature"]
attr_accessor :fruit, :status
def initialize
@rings = 0
@status = "healthy"
@fruit = []
end
def tree_grows
@rings += 1
@stage = STAGES[@rings] || "mature"
end
def check_age
if tree.status = "dead"
return @rings
else
tree_alive_error
end
end
def age!
fruit_spoils
tree_grows
if status == "healthy" && @stage == "mature"
# p "I should be produc'n frust"
@fruit.<<(self.fruit_class.new)
else
end
end
def fruit_spoils
@fruit = [] if fruit[0]
end
end
class AppleTree < Tree
def fruit_class
Apple
end
end
class PearTree < Tree
def fruit_class
Pear
end
end
class OrangeTree < Tree
def fruit_class
Orange
end
end
class Grove
attr_accessor :plants
def initialize
@plants = []
end
def crop
self.plants
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment