Why is define_method private? Plus,what's the point of private methods?

D

Daniel Finnie

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
 
J

Justin Collins

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

Gregory Brown

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?

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

Logan Capaldo

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

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top