Is singleton class of an object already created?

Discussion in 'Ruby' started by Samnang Chhun, Jan 4, 2011.

  1. I would like to know is there any ways to check is singleton class of an
    object already created?

    ex: obj.singleton_class_defined?

    --
    Posted via http://www.ruby-forum.com/.
    Samnang Chhun, Jan 4, 2011
    #1
    1. Advertising

  2. Samnang Chhun wrote in post #972157:
    > I would like to know is there any ways to check is singleton class of an
    > object already created?
    >
    > ex: obj.singleton_class_defined?


    As an indirect test, you can't Marshal any object with a singleton class
    (although there are other kinds of object that you can't Marshal either)

    irb(main):001:0> foo = "abc"
    => "abc"
    irb(main):002:0> (Marshal.dump(foo) && false) rescue true
    => false
    irb(main):003:0> def foo.zzz; end
    => nil
    irb(main):004:0> (Marshal.dump(foo) && false) rescue true
    => true

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 4, 2011
    #2
    1. Advertising

  3. > I would like to know is there any ways to check is singleton class of an
    > object already created?


    Perhaps you need a method to determine the presence of singleton
    methods. In Ruby 1.9.2 you have the Object#singleton_methods that
    returns an array of singleton methods defined on the object.

    Flanagan, and Matz's book always talks about "opening" the eigenclass,
    and not defining one. I guess that a singleton class always exists for
    an object, whether a singleton method for that object exists or not.
    You can always get a reference to the eigenclass of the object with
    "class << object; self; end", where object can be any object (that
    allows singleton methods to be defined on them).

    --
    Anurag Priyam
    Anurag Priyam, Jan 4, 2011
    #3
  4. Anurag Priyam wrote in post #972264:
    I guess that a singleton class always exists for
    > an object, whether a singleton method for that object exists or not.


    Sure, singleton class always be there, and it inserted in the
    inheritance chain before the objects "real" class when the first we need
    it. But I would like to generate a diagram of an object's ancestors, if
    the object has used singleton class, then it will show in diagram, but
    it doesn't if object hasn't used singleton class yet.

    --
    Posted via http://www.ruby-forum.com/.
    Samnang Chhun, Jan 4, 2011
    #4
  5. Samnang Chhun

    timr Guest

    Could you simply count singleton_methods? If singleton_methods returns
    an empty array, no singleton_class is present. Could easily add that
    method to the object class and call it on anything you wanted. But
    this strategy equates an empty singlton_method query with not having a
    singleton_class. I am not sure that is exactly correct. Though
    practically speaking, it seems equivalent to me.
    Tim

    class Object
    def singleton_class?
    self.singleton_methods.length == 0 ? false : true
    end
    end

    a = ''
    p a.singleton_class?
    => false

    def a.singleton_method_for_a
    end
    p a.singleton_class?
    => true




    On Jan 4, 9:28 am, Samnang Chhun <> wrote:
    > Anurag Priyam wrote in post #972264:
    > I guess that a singleton class always exists for
    >
    > > an object, whether a singleton method for that object exists or not.

    >
    > Sure, singleton class always be there, and it inserted in the
    > inheritance chain before the objects "real" class when the first we need
    > it. But I would like to generate a diagram of an object's ancestors, if
    > the object has used singleton class, then it will show in diagram, but
    > it doesn't if object hasn't used singleton class yet.
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.
    timr, Jan 4, 2011
    #5
  6. I'm not sure every time we invoke 'singleton_methods', then it will
    create the singleton class if it doesn't exist yet.

    --
    Posted via http://www.ruby-forum.com/.
    Samnang Chhun, Jan 5, 2011
    #6
  7. On Wed, Jan 5, 2011 at 2:36 AM, Samnang Chhun <> wrote:
    > I'm not sure every time we invoke 'singleton_methods', then it will
    > create the singleton class if it doesn't exist yet.


    Apparently it doesn't according to Brian's test:

    irb(main):001:0> x="foo"
    => "foo"
    irb(main):002:0> Marshal.dump(x)
    => "\x04\bI\"\bfoo\x06:\rencoding\"\nCP850"
    irb(main):003:0> x.singleton_methods
    => []
    irb(main):004:0> Marshal.dump(x)
    => "\x04\bI\"\bfoo\x06:\rencoding\"\nCP850"
    irb(main):005:0> def x.a;end
    => nil
    irb(main):006:0> Marshal.dump(x)
    TypeError: singleton can't be dumped
    from (irb):6:in `dump'
    from (irb):6
    from /opt/bin/irb19:12:in `<main>'
    irb(main):007:0>

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jan 5, 2011
    #7
  8. [Note: parts of this message were removed to make it a legal post.]

    On Tue, Jan 4, 2011 at 9:00 PM, timr <> wrote:

    > class Object
    > def singleton_class?
    > self.singleton_methods.length == 0 ? false : true
    > end
    > end
    >


    Really small point here, and not really on topic, but this is a little
    around the houses and can be slightly improved:

    class Object
    def singleton_class?
    !self.singleton_methods.empty?
    end
    end

    Or just != 0, since it'll return the true/false without the need for a
    ternary.
    Adam Prescott, Jan 5, 2011
    #8
  9. On Wed, Jan 5, 2011 at 11:24 AM, Adam Prescott <> wrote=
    :
    > On Tue, Jan 4, 2011 at 9:00 PM, timr <> wrote:
    >
    >> class Object
    >> =A0def singleton_class?
    >> =A0 =A0self.singleton_methods.length =3D=3D 0 ? false : true
    >> =A0end
    >> end
    >>

    >
    > Really small point here, and not really on topic, but this is a little
    > around the houses and can be slightly improved:


    I couldn't agree more! Using ternary operator with "false" and "true"
    is not needed most of the time. (Only exception I would concede is to
    avoid leakage of references - but then you can as well do "!! expr".)

    > class Object
    > =A0def singleton_class?
    > =A0 =A0!self.singleton_methods.empty?
    > =A0end
    > end
    >
    > Or just !=3D 0, since it'll return the true/false without the need for a
    > ternary.


    Or just

    class Object
    def singleton_class?
    !singleton_methods.empty?
    end
    end

    Now we can wonder whether this is worth a method at all... :)

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jan 5, 2011
    #9
  10. Samnang Chhun

    Su Zhang Guest

    I just found that even though a singleton method is removed, the
    receiver is still considered a singleton and cannot be dumped.

    ---------------

    foo = Object.new

    def foo.bar
    'foo.bar'
    end

    p foo.singleton_methods # => [:bar]

    class << foo
    undef bar
    end

    p foo.singleton_methods # => []
    Marshal.dump(foo) # error

    --------------

    Looking inside marhsal.c, the following check routine is found:

    if (RCLASS_M_TBL(klass)->num_entries ||
    (RCLASS_IV_TBL(klass) && RCLASS_IV_TBL(klass)->num_entries > 1)) {
    rb_raise(rb_eTypeError, "singleton can't be dumped");
    }

    It seems that the records remains in some dispatch table even if
    Object#singleton_methods returns an empty array.

    --
    Posted via http://www.ruby-forum.com/.
    Su Zhang, Jan 8, 2011
    #10
  11. Jonas Pfenniger (zimbatm), Jan 8, 2011
    #11
  12. Samnang Chhun

    John Mair Guest

    in YARV and MRI, the singleton class always exists for a class (it is
    created on class creation), but is created lazily for ordinary objects.

    Samnang Chhun wrote in post #972157:
    > I would like to know is there any ways to check is singleton class of an
    > object already created?
    >
    > ex: obj.singleton_class_defined?


    --
    Posted via http://www.ruby-forum.com/.
    John Mair, Jan 9, 2011
    #12
    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,201
    Proton Projects - Moin
    Mar 27, 2007
  2. Josselin
    Replies:
    2
    Views:
    82
  3. Wilhelm
    Replies:
    1
    Views:
    149
  4. Paul McMahon
    Replies:
    3
    Views:
    188
    David A. Black
    Jun 9, 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