Module and Class

Wa =
Wa.class # Module
Wa.superclass # Throw Exception 'NoMethodError' here. 
 # Means 'modules' do not have parents or son. 'modules' are alone. 
 # Only can be included or include other 'modules'. # Throw Exception 'NoMethodError' here. Means 'modules' couldn't be initialized.

Wa.module_eval { def a; puts 'a'; end }
Wa.class_eval { def b; puts 'b';end }

Ca =
Ca.module_eval { include Wa } # a # b
 # 'module_eval' is equal to 'class_eval'. 
 # They can be used on 'modules' or 'classes'.
 # They both means 'Open Class'.
Wa = {
  def a; end
  def b; end

Ca =

Mo = {
  include Wa # 'include' method is defined at 'Module' class, 
             # so a module can include another module.

  def c; end

Ca.class_eval do
  include Mo

puts Ca.instance_methods.include?(:a) && Ca.instance_methods.include?(:b) # true

puts Mo.instance_methods.include?(:a) && Mo.instance_methods.include?(:b) # true
  # 'instance_methods' is defined at 'Module', so a 'module' also have use it.
  # So most methods used for 'class' can also be used for 'module'. 

puts Ca.instance_methods.include?(:c) # true
module Mo
  def kk
    @f = 5 # This 'instance_variable' is used for a initialized Class. 
           # Module could not be initialized. But we can define 'instance_variable' here.

class Abc
  include Mo

# Abc.include(Mo) # This line is equal to the previous three lines.

a =
a.kk # If not executed, the 'instance_variable' @f is nil.
puts a.instance_variable_get(:@f).inspect # 5
puts Abc.instance_methods.grep(/k/).inspect # [:kk, :kind_of?]
puts Abc.instance_methods == a.methods # true
puts Class.instance_methods == Abc.methods # true

inherited = false
Class.instance_methods(inherited) # [:allocate, :new, :superclass]

class Bc < Abc
  define_method(:abc) do
    # Here we entered the scope of instance! The same as in method :abcd .
  def abcd
    # Here we entered the scope of instance! The same as in method :abc .

puts Bc.superclass == Abc # true

# In top-level env, 'self' is object 'main'. 
self.respond_to?(:define_method, true) 
# Return true. But 'define_method' method is defined at 'Module' class, 
# so only modules have method 'define_method'. 'main' is a special object. Matz just want to make programming easy, so he make 'main' different,
# then you can use the defined method after the method defined in main. 
class GGG

ggg =

ggg.instance_eval do
  def kkkkgg
    puts "kkkk"

ggg.kkkkgg # kkkk. So 'instance_eval' can enter object's 'eigenclass' scope. 
 # This is different form 'class_eval'. 'class_eval' is defined in 'Module', but
 # 'instance_eval' is defined in 'Object'. 
 # 'class_eval' means open class, the method defined in 'class_eval' is used for instance. 
# These below looks like 'Classes', because they are upper cased first letter. 
# But actually they are methods. They are defined in 'Kernel' module, so they can be used by any objects.
# puts self.private_methods # Then you can see them.
module Rack
  # Here the code in file 'rack/builder' is not executed until you use the class 'Rack::Builder'.
  autoload :Builder, "rack/builder"
  # But 'require' is different from 'autoload' because it will run the code of "something" immediately.
  # So 'rack' use 'autoload' to announce load all the 'modules' it needs but not actually load they. 
  require "something"

obj.singleton_class is obj's eigenclass. Let’s prove it.

class Tom
  puts "class Tom's singleton_class is #{self.singleton_class.inspect}"

  class << self
    puts "class Tom's eigenclass is #{self.inspect}"
    puts "#{self == Tom.singleton_class}" # true

tom =

tom_sc = tom.singleton_class

puts "#{tom_sc.inspect} #{tom_sc.object_id}" #<Class:#<Tom:0x00007fe88f14bae8>>70318404820300

tom_sc.instance_eval do
  puts "#{self.inspect} #{self.object_id}" #<Class:#<Tom:0x00007fe88f14bae8>>70318404820300

  puts "#{tom.singleton_class == self}" # true

When I was reading Rails source code in railties/lib/rails/application.rb, I see this code

def instance

What does this mean? It really confused me. After a deep research, I realize that this code is equal to

def instance
  a_returned_value = super # Keyword 'super' will call the parent's same name method. In this example, it is method named 'instance'.

Many people may think super.run_load_hooks! means self.superclass.run_load_hooks! That’s wrong.