Return to Snippet

Revision: 80641
at March 23, 2020 17:21 by chrisaiv


Updated URL
https://www.chrisjmendez.com/2008/06/21/ruby-mixing-in-a-module-immediately-after-an-objects-singleton-class/

Updated Code
https://www.chrisjmendez.com/2008/06/21/ruby-mixing-in-a-module-immediately-after-an-objects-singleton-class/

Updated Description
https://www.chrisjmendez.com/2008/06/21/ruby-mixing-in-a-module-immediately-after-an-objects-singleton-class/

Revision: 80618
at March 23, 2020 05:38 by chrisaiv


Updated URL
https://www.chrisjmendez.com/2020/03/23/ruby-mixing-in-a-module-immediately-after-an-objects-singleton-class/

Updated Code
https://www.chrisjmendez.com/2020/03/23/ruby-mixing-in-a-module-immediately-after-an-objects-singleton-class/

Updated Description
https://www.chrisjmendez.com/2020/03/23/ruby-mixing-in-a-module-immediately-after-an-objects-singleton-class/

Revision: 6863
at June 20, 2008 19:58 by chrisaiv


Initial Code
#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

Initial URL


Initial Description
There are two ways to insert a module above the objects singleton class but before the objects Class

Initial Title
Mixing in a Module immediately after an objects Singleton class

Initial Tags
ruby

Initial Language
Ruby