Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Why is define_method private? Plus,what's the point of private methods?

Reply
Thread Tools

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

 
 
Daniel Finnie
Guest
Posts: n/a
 
      12-15-2006
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

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      12-15-2006
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

 
Reply With Quote
 
 
 
 
Gregory Brown
Guest
Posts: n/a
 
      12-15-2006
On 12/14/06, Justin Collins <(E-Mail Removed)> 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?


Very much agreed. The number one use of private for me is to hide
some_very_long_named_and_probably_not_externally_u seful_method()

I've always thought of it as a documentation and organizational thing,
not a security thing.

 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      12-16-2006
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?
>

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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
private(true) || private :make_time, what does it do? why? Rie! Ruby 3 03-26-2008 12:40 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
RE: Why I use private variables (WAS: RE:"private" variablesa.k.a. name mangling?) Jeremy Bowers Python 3 01-24-2005 10:52 PM



Advertisments