
Untitled
By: a guest on
May 31st, 2012 | syntax:
None | size: 0.63 KB | hits: 13 | expires: Never
##
## with raw object
##
# create a new object
o = Object.new
# define methods on the object (precisely, the object's singleton)
class << o
def hello(thing)
puts "hello #{thing}"
end
end
module Yeller
def hello(thing)
super thing.upcase
end
end
module Queryer
def hello(thing)
super "#{thing}?"
end
end
# apply the Yeller mixin to the object
o.extend Yeller
# apply the Queryer mixin to the object
o.extend Queryer
o.hello "world" #=> hello WORLD?
##
## with classes
##
class Person
include Yeller
include Queryer
def hello(thing)
puts "hello #{thing}"
end
end
Person.new.hello "world" #=> hello WORLD?