The following code works:
class MyClass
def method_a
method_b
end
private
def method_b
puts "Hello!"
end
end
m = MyClass.new
m.method_a
Changing the call to method_b to self.method_b however does not work:
def method_a
self.method_b
end
I get a NoMethodError. I’m under the impression that self just resolves to the instance of the class when inside an instance method. Why does self.method_b cause problems?
Note: self.method_b works when private is changed to protected.
Note: if the above methods are changed to class methods then calling self.method_b from method_a doesn’t throw the NoMethodError.
This is how
privatemethods work in Ruby. They cannot be called with an explicit receiver (unless it is a setter method; see below).Read more in the section on Access Control from the Pickaxe.
Private methods whose names end with an
=may be invoked usingself.method_name = ...as this is necessary to differentiate them from setting a local variable: