[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

un_metaclass

Ara.T.Howard

5/3/2005 5:22:00 PM

13 Answers

Robert Klemme

5/3/2005 5:46:00 PM

0

Ara.T.Howard wrote:
> thoughts on how to do this
>
> class C
> class << self
> p un_metaclass #=> C
> end
> end
>
> ??

I'd prefer to call it #instance 'cause that's what it is. :-)

Kind regards

robert

Ara.T.Howard

5/3/2005 6:05:00 PM

0

Ilmari Heikkinen

5/3/2005 6:46:00 PM

0


On 3.5.2005, at 21:24, Ara.T.Howard@noaa.gov wrote:

> On Tue, 3 May 2005, Robert Klemme wrote:
>
>> Ara.T.Howard wrote:
>>> thoughts on how to do this
>>>
>>> class C
>>> class << self
>>> p un_metaclass #=> C
>>> end
>>> end
>>>
>>> ??
>>
>> I'd prefer to call it #instance 'cause that's what it is. :-)
>
>
> hmmm. ok. any ideas? i'm playing with nesting and ancestors - but
> no luck.
>

There's this terrible hack:

class Class
def un_metaclass
to_s.scan(/:[^A-Z]*(([A-Za-z0-9]*(::)?)+)/)[0][0].
split("::").inject(Object){|m, n| m.const_get(n)}
end
end

It's not something I'd like to use though :-)



Ara.T.Howard

5/3/2005 6:49:00 PM

0

Ara.T.Howard

5/3/2005 7:12:00 PM

0

dave.burt

5/3/2005 11:05:00 PM

0

# This is how to do it:

class Module
def instances
result = []
ObjectSpace.each_object do |obj|
result << obj if obj.is_a? self
end
result
end
end

Mark Hubbart

5/3/2005 11:30:00 PM

0

On 5/3/05, Ara.T.Howard <Ara.T.Howard@noaa.gov> wrote:
>
> thoughts on how to do this
>
> class C
> class << self
> p un_metaclass #=> C
> end
> end
>
> ??

heh... I was ahead of you on that one :) in the "inside_metaclass?"
thread, my solution returned the instance for the "true" value:

http://rubytalk....

class Class
def metaclass?
id = inspect[/\A\#<Class:\#<.+?\:0x(.+?)>>\Z/, 1]
ObjectSpace._id2ref(id.to_i(16)/2) if id
end
end

Note that this code is very fragile; it will not work on 1.6.x, and
isn't guaranteed to work in the future. It extracts the object_id of
the instance from the metaclass' inspect string.

cheers,
Mark



Ara.T.Howard

5/4/2005 2:25:00 AM

0

Ara.T.Howard

5/4/2005 2:27:00 AM

0

Mark Hubbart

5/4/2005 6:05:00 AM

0

On 5/3/05, Ara.T.Howard@noaa.gov <Ara.T.Howard@noaa.gov> wrote:
> On Wed, 4 May 2005, Mark Hubbart wrote:
>
> > On 5/3/05, Ara.T.Howard <Ara.T.Howard@noaa.gov> wrote:
> >>
> >> thoughts on how to do this
> >>
> >> class C
> >> class << self
> >> p un_metaclass #=> C
> >> end
> >> end
> >>
> >> ??
> >
> > heh... I was ahead of you on that one :) in the "inside_metaclass?"
> > thread, my solution returned the instance for the "true" value:
> >
> > http://rubytalk....
> >
> > class Class
> > def metaclass?
> > id = inspect[/\A\#<Class:\#<.+?\:0x(.+?)>>\Z/, 1]
> > ObjectSpace._id2ref(id.to_i(16)/2) if id
> > end
> > end
> >
> > Note that this code is very fragile; it will not work on 1.6.x, and
> > isn't guaranteed to work in the future. It extracts the object_id of
> > the instance from the metaclass' inspect string.
>
> it doesn't seem to work for me:
>
> jib:~/eg/ruby > cat a.rb
>
> class Module
> def instance_class
> m = %r/^(?:#<Class:)+(.*?)(>+)$/io.match inspect
> if m
> klass_name, brackets = m[1], m[2]
> n = brackets.size - 1
> klass = const_get klass_name
> n.times{ klass = class << klass; self; end }
> klass
> else
> superclass
> end
> end
> def metaclass?
> id = inspect[%r/\A\#<Class:\#<.+?\:0x(.+?)>>\Z/, 1]
> ObjectSpace._id2ref(id.to_i(16)/2) if id
> end
> def both
> [ instance_class, metaclass? ]
> end
> end
>
> class C
> p both
> class << self
> p both
> class << self
> p both
> class << self
> p both
> end
> end
> end
> end
>
> class K < C
> p both
> class << self
> p both
> class << self
> p both
> class << self
> p both
> end
> end
> end
> end
>
> jib:~/eg/ruby > ruby a.rb
> [Object, nil]
> [C, nil]
> [#<Class:C>, nil]
> [#<Class:#<Class:C>>, nil]
> [C, nil]
> [K, nil]
> [#<Class:K>, nil]
> [#<Class:#<Class:K>>, nil]
>
> did i do something wrong?

Ugh. I hadn't thought about the metaclasses of classes, or the
metaclasses of metaclasses of classes. The code will work for any
non-class object, though, I think.

I'm not sure how to do this. Some ugly hacking might make it work, but
it wouldn't be correct. It can probably only be done correctly with C
or DL. I hated parsing the inspect output anyway, there was something
just *wrong* about that. :)

cheers,
Mark