Singleton class - instance or class variables?

Discussion in 'Ruby' started by Caleb Tennis, Dec 20, 2005.

  1. Caleb Tennis

    Caleb Tennis Guest

    When using a singleton class (one that includes Singleton, not an
    eigenclass), can anyone think of any advantage/disadvantage to using
    class variables over instance variables (or visa versa) to access
    data in the class?

    Thanks,
    Caleb
    Caleb Tennis, Dec 20, 2005
    #1
    1. Advertising

  2. Caleb Tennis wrote:
    > When using a singleton class (one that includes Singleton, not an
    > eigenclass), can anyone think of any advantage/disadvantage to using
    > class variables over instance variables (or visa versa) to access
    > data in the class?


    I would recommend using class instance variables,
    but the only reason I can think relates to resource
    control and instance instantiation. For the typical
    case one could just as well use classes and class
    instance methods.

    > Thanks,
    > Caleb



    E

    --
    Posted via http://www.ruby-forum.com/.
    Eero Saynatkari, Dec 20, 2005
    #2
    1. Advertising

  3. Caleb Tennis wrote:
    > When using a singleton class (one that includes Singleton, not an
    > eigenclass), can anyone think of any advantage/disadvantage to using
    > class variables over instance variables (or visa versa) to access data
    > in the class?


    In answer to this question and the one about thread-specific singletons:
    another approach to consider is using a dependency injection framework.
    They all support singleton services, and at least some of them support
    services that return one unique object per thread.

    One disadvantage to using class variables (@@var) is that they are (in
    the current ruby) per-hierarchy, not per class. So a subclass will share
    the value of @@var. This is, I've heard, going to change in ruby 2.0.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Dec 20, 2005
    #3
  4. Caleb Tennis

    Guest

    On Tue, 20 Dec 2005, Caleb Tennis wrote:

    > When using a singleton class (one that includes Singleton, not an
    > eigenclass), can anyone think of any advantage/disadvantage to using class
    > variables over instance variables (or visa versa) to access data in the
    > class?


    inheritence will break with either.

    with @@var then

    class Singleton
    @@var = 42
    end

    class SingletonII < Singleton
    @@var = 'forty-two'
    end

    class Singleton
    p @@var #=> 'forty-two'
    end

    but with @var

    class Singleton
    @var = 42
    end

    class SingletonII < Singleton
    end

    class SingletonII
    p @var #=> nil
    end

    my traits lib solves this - if inheritence of class data is important. it may
    not be.

    regards.

    -a
    --
    ===============================================================================
    | ara [dot] t [dot] howard [at] noaa [dot] gov
    | all happiness comes from the desire for others to be happy. all misery
    | comes from the desire for oneself to be happy.
    | -- bodhicaryavatara
    ===============================================================================
    , Dec 20, 2005
    #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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,216
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    154
  3. Paul McMahon
    Replies:
    3
    Views:
    196
    David A. Black
    Jun 9, 2008
  4. Ralph Shnelvar
    Replies:
    29
    Views:
    739
    David Masover
    Nov 30, 2009
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

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

Share This Page