Calling private method in ruby

A

Alin Popa

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
 
H

Hakusa

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

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.
 
S

Stefano Crocco

Alle venerd=C3=AC 29 giugno 2007, Alin Popa ha scritto:
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 =3D 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

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

class MyClass
=20
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 =3D 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
=2D--
=2E/script.rb:20: private method `private_method' called for=20
#<MyClass:0xb7c171d8> (NoMethodError)

Note that no receiver is allowed for private method, not even self. So,=20
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
 
A

Alin Popa

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
 
D

Daniel DeLorme

Alin said:
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
 
A

Alin Popa

Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top