class/singleton methods

Discussion in 'Ruby' started by Mark Volkmann, Oct 3, 2005.

  1. These seem to be identical. Are they?

    class Foo
    def Foo.bar
    # some code
    end
    end

    class Foo
    def self.bar
    # some code
    end
    end

    If they are identical, is one form generally preferred over the other?

    So a "class method" is the same as a "singleton method" on a Class object?

    --
    R. Mark Volkmann
    Partner, Object Computing, Inc.
    Mark Volkmann, Oct 3, 2005
    #1
    1. Advertising

  2. Mark Volkmann

    Rob Rypka Guest

    On 10/2/05, Mark Volkmann <> wrote:
    > These seem to be identical. Are they?
    >
    > class Foo
    > def Foo.bar
    > # some code
    > end
    > end
    >
    > class Foo
    > def self.bar
    > # some code
    > end
    > end


    They are identical. Inside Foo, self=3D=3DFoo

    > If they are identical, is one form generally preferred over the other?


    I think it's more a matter of style. The advantage of using 'self' is
    that it always works with the class name (so you won't mess up cutting
    and pasting or changing the name of the class), but it's less clear
    what it does to others (you had to ask).

    > So a "class method" is the same as a "singleton method" on a Class object=

    ?

    Umm, yes? I'm not always sure what those terms refer to.

    I think reading this will help sort out your issues:

    http://www.rubygarden.org/ruby?ClassMethods

    --
    Rob
    Rob Rypka, Oct 3, 2005
    #2
    1. Advertising

  3. Hi --

    On Mon, 3 Oct 2005, Mark Volkmann wrote:

    > These seem to be identical. Are they?
    >
    > class Foo
    > def Foo.bar
    > # some code
    > end
    > end
    >
    > class Foo
    > def self.bar
    > # some code
    > end
    > end
    >
    > If they are identical, is one form generally preferred over the other?


    The advantage of the self version is that it might be a little easier
    to maintain (e.g., if you change the name of the class).

    > So a "class method" is the same as a "singleton method" on a Class object?


    Basically, yes. I think the main difference is that they have a bit
    of language-level "special case" status. There's the class_methods
    method, which makes them "official". Also, because of the way
    classes' singleton classes work, a subclass can actually call the
    singleton methods of its parent class:

    class C
    def self.x
    puts "Calling x on #{self}"
    end
    end

    class D < C
    end

    D.x # Calling x on D

    As far as I know that's the only situation where an object can call
    another object's singleton method. That may be enough to warrant a
    different term for them. I have to say, though, I've seen the class
    method/singleton method relation serve as the "ah ha!" moment for
    many, many people grappling with singleton stuff in Ruby (even if it
    turns out to be a somewhat special case).


    David

    --
    David A. Black
    David A. Black, Oct 3, 2005
    #3
  4. Mark Volkmann

    ES Guest

    David A. Black wrote:
    > Hi --
    >
    > On Mon, 3 Oct 2005, Mark Volkmann wrote:
    >
    >> These seem to be identical. Are they?
    >>
    >> class Foo
    >> def Foo.bar
    >> # some code
    >> end
    >> end
    >>
    >> class Foo
    >> def self.bar
    >> # some code
    >> end
    >> end
    >>
    >> If they are identical, is one form generally preferred over the other?

    >
    >
    > The advantage of the self version is that it might be a little easier
    > to maintain (e.g., if you change the name of the class).
    >
    >> So a "class method" is the same as a "singleton method" on a Class
    >> object?

    >
    >
    > Basically, yes. I think the main difference is that they have a bit
    > of language-level "special case" status. There's the class_methods
    > method, which makes them "official". Also, because of the way
    > classes' singleton classes work, a subclass can actually call the
    > singleton methods of its parent class:
    >
    > class C
    > def self.x
    > puts "Calling x on #{self}"
    > end
    > end
    >
    > class D < C
    > end
    >
    > D.x # Calling x on D
    >
    > As far as I know that's the only situation where an object can call
    > another object's singleton method.


    Hm?

    class Foo
    end

    f = Foo.new

    def f.foo()
    puts 'foo'
    end

    f.foo


    > ... That may be enough to warrant a
    > different term for them. I have to say, though, I've seen the class
    > method/singleton method relation serve as the "ah ha!" moment for
    > many, many people grappling with singleton stuff in Ruby (even if it
    > turns out to be a somewhat special case).
    >
    >
    > David


    E
    ES, Oct 3, 2005
    #4
  5. ES wrote:
    >> As far as I know that's the only situation where an object can call
    >> another object's singleton method.


    Meaning that the singleton method of C is being called with self as D.

    My apologies if I misinterpret you David.
    Nicholas Seckar, Oct 3, 2005
    #5
  6. Hi --

    On Mon, 3 Oct 2005, ES wrote:

    > David A. Black wrote:
    >>
    >> As far as I know that's the only situation where an object can call
    >> another object's singleton method.

    >
    > Hm?
    >
    > class Foo
    > end
    >
    > f = Foo.new
    >
    > def f.foo()
    > puts 'foo'
    > end
    >
    > f.foo


    I think you must have misunderstood me. What I mean is: as far as I
    know, class methods are the only per-object methods (defined as
    obj.meth) that an object other than the object they were defined on
    has in its method search path.


    David

    --
    David A. Black
    David A. Black, Oct 3, 2005
    #6
  7. On 10/2/05, David A. Black <> wrote:
    > Hi --
    >
    > On Mon, 3 Oct 2005, Mark Volkmann wrote:
    >
    > > These seem to be identical. Are they?
    > >
    > > class Foo
    > > def Foo.bar
    > > # some code
    > > end
    > > end
    > >
    > > class Foo
    > > def self.bar
    > > # some code
    > > end
    > > end
    > >
    > > If they are identical, is one form generally preferred over the other?

    >
    > The advantage of the self version is that it might be a little easier
    > to maintain (e.g., if you change the name of the class).
    >
    > > So a "class method" is the same as a "singleton method" on a Class obje=

    ct?
    >
    > Basically, yes. I think the main difference is that they have a bit
    > of language-level "special case" status. There's the class_methods
    > method, which makes them "official".


    Did you mean the singleton_methods method? I don't see a method named
    class_methods anywhere.

    --
    R. Mark Volkmann
    Partner, Object Computing, Inc.
    Mark Volkmann, Oct 3, 2005
    #7
  8. Hi --

    On Mon, 3 Oct 2005, Mark Volkmann wrote:

    > On 10/2/05, David A. Black <> wrote:
    >>
    >> Basically, yes. I think the main difference is that they have a bit
    >> of language-level "special case" status. There's the class_methods
    >> method, which makes them "official".

    >
    > Did you mean the singleton_methods method? I don't see a method named
    > class_methods anywhere.


    I meant class_methods, and I was wrong. For some reason, I have for
    five years been going repeatedly through the cycle of thinking there
    is such a method, and then being reminded that there isn't, and then
    thinking that there is. It's some kind of bizarre mental glitch.

    I'm actually glad there isn't :) And maybe this time I'll remember.


    David

    --
    David A. Black
    David A. Black, Oct 3, 2005
    #8
    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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,202
    Proton Projects - Moin
    Mar 27, 2007
  2. Paul McMahon
    Replies:
    3
    Views:
    189
    David A. Black
    Jun 9, 2008
  3. Kenneth McDonald
    Replies:
    5
    Views:
    289
    Kenneth McDonald
    Sep 26, 2008
  4. Daniel DeLorme
    Replies:
    14
    Views:
    234
    Brian Candler
    Dec 14, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    191
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page