Created
October 28, 2011 19:18
-
-
Save waffle2k/1323207 to your computer and use it in GitHub Desktop.
Playing with class methods and such
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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