- There are all (instance) methods and no standalone functions in Ruby
- Instance methods: methods can only be called from an instance and defined in the class of the instance
- methods defined in class objects are bytecodes without binded self object
obj.foo(*args, &block)meansobj.class.instance_method(:foo).bind(obj).call(*args, &block)foo(*args, &block)meansself.foo(*args, &block)
- All methods are defined inside a Class object
- a Class is an object (i.e. an instance of class Class)
- classes are singletons (because constants are singletons)
- All method lookups strictly follows the lookup chain provided above
supermeans lookup and invoke the method with the same name and same arguments in the lookup chain one level above- chain from
M0to the very end of the chain is shared across all instances ofobj.class. (get fromobj.class.ancestors) - chain from
obj.singleton_classtillobj.classis exclusive forobj(get fromobj.singleton_class.included_modules) - the exact inserting place of
include,prependandextendis given above (IMPORTANT: it's from the perspective ofobj.class!)
- Common tricks
- How to change
self:instance_eval - How to specify a certain method object to call?
obj.to_sv.s.::Kernel.instance_method(:to_s).bind(obj).call - Where does this method defined?
obj.method(:foo).owner #=> of type Module - Method#source_location
- Method#source
- How to change
class X
def X.foo; end
def self.foo; end # self is X here
class << self # reopen X.singleton_class
def foo; end
end
end
def X.foo; end
X.extend(Module.new do
def foo; end
end)class X
def foo; :base; end
end
X.prepend(Module.new do
def foo
puts "before"
super
puts "after"
end
end)Q1: How to override a class method?
X.extend(Module.new do
def class_var
@class_var ||= []
end
end)


I've made a diagram based on this:
The
rubycode:The output: