Object to which a meta class belongs?

Discussion in 'Ruby' started by Erik Veenstra, Jul 24, 2006.

  1. If a singleton class belongs to an object, it should be
    possible to determine to which object it belongs. Is that
    possible?

    I came up with the code below, but it might not be very fast.

    Thanks.

    gegroet,
    Erik V. - http://www.erikveen.dds.nl/

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

    class Object
    def metaclass
    obj = self

    class << self
    self
    end.instance_eval do
    instance_eval <<-END
    def belongs_to
    ObjectSpace._id2ref(#{obj.__id__})
    end
    END

    self
    end
    end
    end

    a = "TEST"

    p a.__id__
    p a.metaclass.belongs_to.__id__

    ----------------------------------------------------------------
     
    Erik Veenstra, Jul 24, 2006
    #1
    1. Advertising

  2. On 7/24/06, Erik Veenstra <> wrote:
    > If a singleton class belongs to an object, it should be
    > possible to determine to which object it belongs. Is that
    > possible?
    >
    > I came up with the code below, but it might not be very fast.
    >
    > Thanks.
    >
    > gegroet,
    > Erik V. - http://www.erikveen.dds.nl/
    >
    > ----------------------------------------------------------------
    >
    > class Object
    > def metaclass
    > obj = self
    >
    > class << self
    > self
    > end.instance_eval do
    > instance_eval <<-END
    > def belongs_to
    > ObjectSpace._id2ref(#{obj.__id__})
    > end
    > END
    >
    > self
    > end
    > end
    > end
    >
    > a = "TEST"
    >
    > p a.__id__
    > p a.metaclass.belongs_to.__id__
    >
    > ----------------------------------------------------------------
    >
    >
    >


    How about this?

    class Object
    def singleton_of
    ObjectSpace.each_object(self) do |obj|
    return obj
    end
    end
    end

    Regards,
    Sean
     
    Sean O'Halpin, Jul 25, 2006
    #2
    1. Advertising

  3. It does work work, but...

    ... It's even slower. Much slower. ;]

    Thanks.

    gegroet,
    Erik V. - http://www.erikveen.dds.nl/

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

    CPU ELAPSED COUNT CPU/COUNT LABEL
    5.850000 6.708499 1 5.850000 :singleton_of
    0.560000 1.097820 1 0.560000 :belongs_to

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

    class Module
    def singleton_of
    res = nil
    ObjectSpace.each_object(self) do |obj|
    res = obj
    end
    res
    end
    end

    ----------------------------------------------------------------
     
    Erik Veenstra, Jul 25, 2006
    #3
  4. On 7/25/06, Erik Veenstra <> wrote:
    > It does work work, but...
    >
    > ... It's even slower. Much slower. ;]
    >
    > Thanks.
    >
    > gegroet,
    > Erik V. - http://www.erikveen.dds.nl/


    Whoops! Knew I should have benchmarked it. :)
    ObjectSpace.each_object really is a sledgehammer...

    Regards,
    Sean
     
    Sean O'Halpin, Jul 25, 2006
    #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. Stephan
    Replies:
    0
    Views:
    338
    Stephan
    Dec 2, 2003
  2. Joel
    Replies:
    4
    Views:
    9,272
    John Harrison
    Oct 11, 2004
  3. Markus Gaelli
    Replies:
    3
    Views:
    117
    Rick DeNatale
    Sep 2, 2006
  4. Jan Lelis
    Replies:
    22
    Views:
    295
    Robert Klemme
    Jul 13, 2010
  5. Zhang Weiwu
    Replies:
    0
    Views:
    75
    Zhang Weiwu
    Jan 5, 2014
Loading...

Share This Page