Singleton classes and Singleton pattern

Discussion in 'Ruby' started by Wilhelm, Oct 11, 2006.

  1. Wilhelm

    Wilhelm Guest

    I just happened upon singleton classes, and was wondering two things:

    1) There's no difference between these two codes, right? Is there an
    advantage or disadvantage over either? Does Ruby put bar() into the
    same virtual class object for both?

    class Foo
    class << self
    def bar
    end
    end
    end

    class Foo
    def self.bar
    end
    end

    2) A singleton class has nothing to do with the singleton design
    pattern, right? They only share the same name because there is only
    ever one of them.
    Wilhelm, Oct 11, 2006
    #1
    1. Advertising

  2. Wilhelm

    Guest

    Hi --

    On Wed, 11 Oct 2006, Wilhelm wrote:

    > I just happened upon singleton classes, and was wondering two things:
    >
    > 1) There's no difference between these two codes, right? Is there an
    > advantage or disadvantage over either? Does Ruby put bar() into the
    > same virtual class object for both?
    >
    > class Foo
    > class << self
    > def bar
    > end
    > end
    > end
    >
    > class Foo
    > def self.bar
    > end
    > end


    See the thread from a couple of days ago called '"def self.method" vs
    "class << self; def method"'.

    > 2) A singleton class has nothing to do with the singleton design
    > pattern, right? They only share the same name because there is only
    > ever one of them.


    That's correct. The predicate "singleton" applies to both, but
    they're not the same as each other.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
    , Oct 11, 2006
    #2
    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. =?Utf-8?B?RGlmZmlkZW50?=
    Replies:
    5
    Views:
    440
    Bruce Barker
    Jan 13, 2006
  2. Replies:
    4
    Views:
    2,485
  3. Vinesh S
    Replies:
    4
    Views:
    398
    Vinesh S
    Aug 22, 2011
  4. Hal Fulton
    Replies:
    7
    Views:
    147
  5. Trans
    Replies:
    12
    Views:
    261
    Robert Klemme
    Sep 14, 2007
Loading...

Share This Page