Calling private method in ruby

Hi there,

I have a little problem regarding private methods in Ruby.
I have the following case:

class MyClass
private
def my_method
puts “something from my method”
end
end

myClass = MyClass.new
begin
myClass.my_method
rescue
puts “Method cannot be called because is private”
end

myClass.send(“my_method”)

So, can anyone help me by explaining in a logical way, what is the point
of private methods in ruby since I can call it using “send” ?

Thanks in advance,

Alin

On Jun 29, 2:31 am, Alin P. [email protected] wrote:

end
So, can anyone help me by explaining in a logical way, what is the point
of private methods in ruby since I can call it using “send” ?

Thanks in advance,

Alin


Posted viahttp://www.ruby-forum.com/.

Maybe the fact that you have to use send will at least make you
appreciate that the method IS private in a real world scenario. But to
be honest, I think that making methods private is usually unnecessary
to begin with, but it does help with something I’ve forgotten about.
And because you’re getting at the method through another method, it’ll
take longer to act on it; not excruciatingly longer, though.

Alle venerdì 29 giugno 2007, Alin P. ha scritto:

end
So, can anyone help me by explaining in a logical way, what is the point
of private methods in ruby since I can call it using “send” ?

Thanks in advance,

Alin

In general, the idea behind private methods is that they can’t be called
from
outside the object they belong to. In ruby, this is achieved by allowing
calling them only without an explicit receiver. This means that the you
can
call them when the receiver of the method is the implicit receiver
(self),
that is, only from within instance methods:

class MyClass

def private_method
puts “This is private_method”
end
private :private_method

def test_method
puts “This test method is abouto to call private_method”
private_method
end

end

obj = MyClass.new
obj.test_method
puts “—”
obj.private_method

The output is:
This test method is abouto to call private_method
This is a private method

./script.rb:20: private method `private_method’ called for
#MyClass:0xb7c171d8 (NoMethodError)

Note that no receiver is allowed for private method, not even self. So,
test_method couldn’t have been written as:

def test_method
puts “This test method is abouto to call private_method”
self.private_method
end

Doing this would again result in a NoMethodError exception.

I hope this helps

Stefano

Thanks Stefano,

But my issue is not “what is a private method”.
I’m curios about “send” method, why there is a workaround to get to
private methods from OUTSIDE when this shouldn’t happen ?

Alin

Alin P. wrote:

Thanks Stefano,

But my issue is not “what is a private method”.
I’m curios about “send” method, why there is a workaround to get to
private methods from OUTSIDE when this shouldn’t happen ?

You can also do obj.instance_eval{ privmethod() }

This is because ruby is a language that ALLOWS you to do things, not
RESTRICT you from doing things. It’s a matter of philosophy.

Daniel

Daniel DeLorme wrote:

Alin P. wrote:

Thanks Stefano,

But my issue is not “what is a private method”.
I’m curios about “send” method, why there is a workaround to get to
private methods from OUTSIDE when this shouldn’t happen ?

You can also do obj.instance_eval{ privmethod() }

This is because ruby is a language that ALLOWS you to do things, not
RESTRICT you from doing things. It’s a matter of philosophy.

Daniel

Thank you Daniel,

I think you cleared my problem.

Alin