When is Ruby self refering to the Object and when is self refering to the Ruby class? Explanations with examples would be great. Not getting my head around this.
My understanding is
self
refers to the class/module. self
refers to the instance.For example,
class A
def method1
self # => instance of A
end
def self.method2
self # => class A
endu
def A.method3
self # => class A
end
end
class << A
def method4
self # => class A
end
end
module B
module_function
def method5
self # => module B
end
end
Exceptions are that instance_eval
, instance_exec
alter self
to the receiver.