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.
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
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 ?
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.