Ruby: Mixing in a Module immediately after an object's Singleton class
There are two ways to insert a Module above the objects singleton class but before the objects Class.
# You can open the class definition body of a singleton class and add instance methods, class methods, and constants.
# You do this by using the 'class' keyword and a CONSTANT
class C
def hello
puts "this is a hello yo."
end
end
module M
def talk
puts "hello."
end
end
# Example 1
example1 = C.new
class << example1
include M
end
# Call the Singleton method
example1.hello
# Call the Module Mix-in
example1.talk
# Example 2
example2 = C.new
example2.extend(M)
example2.talk