Skip to content

Instantly share code, notes, and snippets.

@rvbsanjose
Created October 15, 2012 07:30
Show Gist options
  • Save rvbsanjose/3891207 to your computer and use it in GitHub Desktop.
Save rvbsanjose/3891207 to your computer and use it in GitHub Desktop.
Cookies and oven
class Cookie
def initialize
@baking_time = 0
end
def in_status
"#{name} cookies into the oven."
end
def bake_batch(time)
@baking_time += time
end
def oven_batch
"#{name.capitalize} cookies have been baking for #{@baking_time} minutes. Status: #{bake_status.capitalize}"
end
def bake_status
case @baking_time
when @baking_time..doughy then :doughy
when (doughy+1)..(almost_ready+1) then :almost_ready
when ready..(ready+1) then :ready
else :burned
end
end
end
class Batch
def initialize
@batch = []
end
def make_batch(batch_type)
@batch << batch_type
end
def bake_batch(time)
@batch.each { |batch| batch.bake_batch(time) }
end
def in_status
puts "Putting a batch of #{@batch.count} #{@batch.first.in_status} "
end
def oven_batch
@batch.first.oven_batch
end
end
class Oven
def put_in(batch_type)
@in_oven = batch_type
batch_type.in_status
end
def bake_batch(time)
@in_oven.bake_batch(time)
end
def take_out(batch_type)
@in_oven = nil
end
def empty?(batch_type)
@in_oven.nil? ? "Your oven is empty. Let's bake some cookies!" : "Your oven is not empty."
end
end
class SugarCookie < Cookie
def name
"sugar"
end
def doughy
4
end
def almost_ready
6
end
def ready
8
end
def burned
10
end
end
class PeanutButterCookie < Cookie
def name
"peanut butter"
end
def doughy
8
end
def almost_ready
10
end
def ready
12
end
def burned
14
end
end
puts "\n"
# Make a new batch of cookies
sugar_cookie = Batch.new
5.times { sugar_cookie.make_batch(SugarCookie.new) }
# Make a oven to take and put batches of cookies in
oven = Oven.new
oven.put_in(sugar_cookie)
oven.bake_batch(8)
puts sugar_cookie.oven_batch
oven.take_out(sugar_cookie)
puts oven.empty?(sugar_cookie)
puts "\n"
# Make a new batch of cookies
peanut_butter = Batch.new
8.times { peanut_butter.make_batch(PeanutButterCookie.new) }
# Make a oven to take and put batches of cookies in
oven = Oven.new
oven.put_in(peanut_butter)
oven.bake_batch(11)
puts peanut_butter.oven_batch
puts oven.empty?(peanut_butter)
puts "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment