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

Discussion in 'Ruby' started by Daniel Finnie, Dec 15, 2006.

  1. 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, Dec 15, 2006
    #1
    1. Advertising

  2. Re: Why is define_method private? Plus, what's the point of privatemethods?

    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
     
    Justin Collins, Dec 15, 2006
    #2
    1. Advertising

  3. Re: Why is define_method private? Plus, what's the point of private methods?

    On 12/14/06, Justin Collins <> 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_useful_method()

    I've always thought of it as a documentation and organizational thing,
    not a security thing.
     
    Gregory Brown, Dec 15, 2006
    #3
  4. 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.
     
    Logan Capaldo, Dec 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C++
    Replies:
    23
    Views:
    9,229
  2. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,397
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,996
    Smokey Grindel
    Dec 2, 2006
  4. Rahul
    Replies:
    9
    Views:
    426
    Andrey Tarasevich
    Mar 25, 2008
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,348
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page