class MyClass
def test
puts my_id
puts self.my_id
end
private
def my_id
115
end
end
m = MyClass.new
m.test
This script results in an output:
115
priv.rb:4:in `test': private method `my_id' called for #<MyClass:0x2a50b68> (NoMethodError)
from priv.rb:15:in `<main>'
What is the difference between calling methods from inside with self
keyword and without it?
From my Delphi and C# experience: there was no difference, and self
could be used to avoid name conflict with local variable, to denote that I want to call an instance function or refer to instance variable.