what's different between self.method and class << self?

Discussion in 'Ruby' started by Zhenning Guan, Oct 21, 2008.

  1. we can define a instance method in lots way.
    mostly, I saw people define like this:
    1.
    class A
    def self.class_method
    #some code here....
    end
    end

    2.
    class A
    class << self
    def class_method
    #some code here....
    end
    end
    end


    ==
    the question confused me is what's the advantage of doing this in 2?
    they'are different? which is your choose?
    --
    Posted via http://www.ruby-forum.com/.
     
    Zhenning Guan, Oct 21, 2008
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan <>wrote:

    > we can define a instance method in lots way.
    > mostly, I saw people define like this:
    > 1.
    > class A
    > def self.class_method
    > #some code here....
    > end
    > end
    >
    > 2.
    > class A
    > class << self
    > def class_method
    > #some code here....
    > end
    > end
    > end
    >
    >
    > ==
    > the question confused me is what's the advantage of doing this in 2?
    > they'are different? which is your choose?
    >

    I asked this same question just a couple of weeks ago. The answers I was
    given were:

    Method 2:
    1) Saves some typing
    2) Has a slightly different mechanism for looking up class constants, which
    99.99% of the time won't be noticed.

    Other than that there are no differences between methods 1 & 2.

    --wpd
     
    Patrick Doyle, Oct 21, 2008
    #2
    1. Advertising

  3. Patrick Doyle wrote:
    > On Tue, Oct 21, 2008 at 3:23 AM, Zhenning Guan
    > <>wrote:
    >
    >> class A
    >> they'are different? which is your choose?
    >>

    > I asked this same question just a couple of weeks ago. The answers I
    > was
    > given were:
    >
    > Method 2:
    > 1) Saves some typing
    > 2) Has a slightly different mechanism for looking up class constants,
    > which
    > 99.99% of the time won't be noticed.
    >
    > Other than that there are no differences between methods 1 & 2.
    >
    > --wpd


    what's the meaning of saves some typing????
    can you give me a example?
    --
    Posted via http://www.ruby-forum.com/.
     
    Zhenning Guan, Oct 21, 2008
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    >
    > what's the meaning of saves some typing????
    > can you give me a example?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    > Instead of having to type


    def self.method
    blah
    blah
    blah
    end

    you can type (within the class << self clause)

    def method
    blah
    blah
    blah
    end

    I didn't say it would save you a _lot_ of typing, just some :)

    --wpd
     
    Patrick Doyle, Oct 21, 2008
    #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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    596
    Lonnie Princehouse
    Jul 11, 2005
  2. Bart Kastermans
    Replies:
    6
    Views:
    409
    Bart Kastermans
    Jul 13, 2008
  3. caccolangrifata
    Replies:
    18
    Views:
    404
    Chris Torek
    Jul 22, 2011
  4. Replies:
    6
    Views:
    117
    Vivek
    Jan 25, 2006
  5. Replies:
    7
    Views:
    165
Loading...

Share This Page