Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created October 28, 2011 19:18
Show Gist options
  • Save waffle2k/1323207 to your computer and use it in GitHub Desktop.
Save waffle2k/1323207 to your computer and use it in GitHub Desktop.
Playing with class methods and such
#!/usr/bin/env ruby
class Milk
def self.cost
puts " with Milk"
0.2
end
end
class Sugar
def self.cost
puts " with Sugar"
0.3
end
end
class Cream
def self.cost
puts " with Cream"
0.1
end
end
class Coffee
attr :cost, true
def initialize
@cost = 2
end
def self.with( *args )
coffee_obj = Coffee.new
puts "Creating coffee.. "
args.each do |field|
case field
when :milk
coffee_obj.cost += Milk.cost
when :sugar
coffee_obj.cost += Sugar.cost
when :cream
coffee_obj.cost += Cream.cost
end
end
return coffee_obj
end
end
drink = Coffee.with :milk, :cream, :sugar
print "Cost of coffee: #{drink.cost}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment