[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling private method in ruby

Alin Popa

6/29/2007 6:31:00 AM

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

--
Posted via http://www.ruby-....

5 Answers

Hakusa@gmail.com

6/29/2007 7:08:00 AM

0

On Jun 29, 2:31 am, Alin Popa <alin.p...@gmail.com> wrote:
> 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
>
> --
> Posted viahttp://www.ruby-....

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.

Stefano Crocco

6/29/2007 7:24:00 AM

0

Alle venerdì 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 = 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 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

Alin Popa

6/29/2007 7:34:00 AM

0

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

--
Posted via http://www.ruby-....

Daniel DeLorme

6/29/2007 7:50:00 AM

0

Alin Popa 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

Alin Popa

6/29/2007 7:58:00 AM

0

Daniel DeLorme wrote:
> Alin Popa 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

--
Posted via http://www.ruby-....