Explain private methods?

Hi,
When you use the word “private” to clear private methods - what does it
mean that only code within the object’s methods can access those private
methods? Can someone give me a simple example of this?

Thanks!

On Tue, Feb 15, 2011 at 10:22 AM, Gaba L.
[email protected] wrote:

When you use the word “private” to clear private methods - what does it
mean that only code within the object’s methods can access those private
methods? Can someone give me a simple example of this?

That’s basically correct, yes. Technically it means that the method
cannot be called with an explicit receiver:

class Foo
def pub
return “public method”
end

def call_priv
priv
end

def call_priv_explicitly
self.priv
end

private
def priv
return “private method”
end
end

f = Foo.new
f.pub

=> “public method”

f.priv

=> private method `priv’ called for #Foo:0x10016a1b0 (NoMethodError)

f.call_priv

=> “private method”

f.call_priv_explicitly

=> private method `priv’ called for #Foo:0x100169b98 (NoMethodError)