Why is define_method a private method? I can't recall ever using define_method on the class I was working in, only using class.send(:define_method)....
Personally, I find that the following:
Class.define_method(:foo, &myBlock)
is much cleaner than this:
Class.send(:define_method, :foo, &myBlock)
What are the advantages to having define_method be private? Are there any cons to having it public? I can find one con to having it private and no pros, which leads my to my main question...why is it private?
Also, a quick philosophy question:
What is the point of private methods if you can bypass their private status so easily? I know it isn't meant as a security measure or anything but it just seems silly. The only reason I can think of is having too many methods cluttering up the RDocs if everything was public, but this is easily solved by having a method being private just meaning that it doesn't show up in the docs.
The same arguments apply to protected methods also, IMO.
Now, I know someone will mention that Ruby 1.9 won't allow calling private methods with an explicit receiver through send, but, according to my "research," Kernel#__send and Kernel#funcall will.
(http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l22)
Thanks,
Dan Finnie
Daniel Finnie wrote:
Also, a quick philosophy question:
What is the point of private methods if you can bypass their private status so easily? I know it isn't meant as a security measure or anything but it just seems silly. The only reason I can think of is having too many methods cluttering up the RDocs if everything was public, but this is easily solved by having a method being private just meaning that it doesn't show up in the docs.
The same arguments apply to protected methods also, IMO.
Now, I know someone will mention that Ruby 1.9 won't allow calling private methods with an explicit receiver through send, but, according to my "research," Kernel#__send and Kernel#funcall will.
(http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l22\)
Thanks,
Dan Finnie
About private methods...this is something that comes up every now and then. In my very humble opinion, private methods are just a polite way of separating the API methods from the methods expected to be used only by the object itself. No, it doesn't stop anyone from using them, really, but if you find yourself going against the intentions of the original programmer, you might stop a moment and think about why you are having to do that. Is it a poor API, or is there a better way to do what you are trying to do?
In other words, private methods are, to me, just a nicety, not something integral to the language. I draw that conclusion from the looseness of their enforcement and the lack of emphasis placed upon them. This happens a lot in Ruby. It let's you do what you want, if you really want. I think that's nice of it.
-Justin
Well a class should control it's own methods, just saying
Class.define_method ... without any context looks dangerous. (Using send
even more so). Obviously it is there to be used, but I think the
assumption is that you do this:
Class.class_eval do
define_method(:foo) { ... }
...
end
It looks more like
class Class
def foo
...
end
end
that way anyway.
···
On Fri, Dec 15, 2006 at 09:59:50AM +0900, Daniel Finnie wrote:
Why is define_method a private method? I can't recall ever using
define_method on the class I was working in, only using
class.send(:define_method)....
Personally, I find that the following:
Class.define_method(:foo, &myBlock)
is much cleaner than this:
Class.send(:define_method, :foo, &myBlock)
What are the advantages to having define_method be private? Are there
any cons to having it public? I can find one con to having it private
and no pros, which leads my to my main question...why is it private?
Very much agreed. The number one use of private for me is to hide
some_very_long_named_and_probably_not_externally_useful_method()
I've always thought of it as a documentation and organizational thing,
not a security thing.
···
On 12/14/06, Justin Collins <collinsj@seattleu.edu> wrote:
About private methods...this is something that comes up every now and
then. In my very humble opinion, private methods are just a polite way
of separating the API methods from the methods expected to be used only
by the object itself. No, it doesn't stop anyone from using them,
really, but if you find yourself going against the intentions of the
original programmer, you might stop a moment and think about why you are
having to do that. Is it a poor API, or is there a better way to do what
you are trying to do?