un_metaclass

A

Ara.T.Howard

thoughts on how to do this

class C
class << self
p un_metaclass #=> C
end
end

??

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
R

Robert Klemme

Ara.T.Howard said:
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
 
A

Ara.T.Howard

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.

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
I

Ilmari Heikkinen

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 :)
 
A

Ara.T.Howard

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

this __seems__ ok:

jib:~/eg/ruby/traits/traits-0.0.0 > cat x.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
end

class C
p instance_class
class << self
p instance_class
class << self
p instance_class
class << self
p instance_class
end
end
end
end

class K < C
p instance_class
class << self
p instance_class
class << self
p instance_class
class << self
p instance_class
end
end
end
end

jib:~/eg/ruby/traits/traits-0.0.0 > ruby x.rb
Object
C
#<Class:C>
#<Class:#<Class:C>>
C
K
#<Class:K>
#<Class:#<Class:K>>


i'm thinking 'instance_class' may be better... instance sounds like a
singleton constructor or something...

basically this is a cross between Module#nesting and Class#superclass : it
yield the first class up the inheritence/singleton chain from the caller.

thoughts?

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
A

Ara.T.Howard

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 :)

is that perl? ;-)

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
D

dave.burt

# 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
 
M

Mark Hubbart

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.org/140390

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
 
A

Ara.T.Howard

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.org/140390

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?

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
A

Ara.T.Howard

# 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

it seems to miss much of the time?


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 instances
result = []
ObjectSpace.each_object do |obj|
result << obj if obj.is_a? self
end
result
end
def both
[ instance_class, instances ]
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, []]
[C, [C]]
[#<Class:C>, []]
[#<Class:#<Class:C>>, []]
[C, []]
[K, [K]]
[#<Class:K>, []]
[#<Class:#<Class:K>>, []]


cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
M

Mark Hubbart

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.org/140390

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
 
A

Ara.T.Howard

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.

the only reason i did is that i needed

class C
trait 'a'

class << self
trait 'a'
end
end

to behave differently and both need a handle on 'C' (sorta like superclass) to
do so.
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. :)

yeah i still want a better solution. this is what i use now:

class Class
def inside_metaclass?
self.inspect =~ %r/^#<Class:/ ? true : false
end
def metaclass
if inside_metaclass?
self
else
class << self; self; end
end
end
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
end

it's specific to my need but i'm thinking of putting together an RCR.

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
M

Mark Hubbart

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.

the only reason i did is that i needed

class C
trait 'a'

class << self
trait 'a'
end
end

to behave differently and both need a handle on 'C' (sorta like superclass) to
do so.
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. :)

yeah i still want a better solution. this is what i use now:

class Class
def inside_metaclass?
self.inspect =~ %r/^#<Class:/ ? true : false
end
def metaclass
if inside_metaclass?
self
else
class << self; self; end
end
end
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
end

it's specific to my need but i'm thinking of putting together an RCR.

The problem the the above definition of inside_metaclass? is that it
will incorrectly identify and anonymous class as a metaclass:

foo = Class.new
foo.class_eval do
puts inside_metaclass?
end

prints "true", which is wrong.

cheers,
Mark
 
A

Ara.T.Howard

foo = Class.new
foo.class_eval do
puts inside_metaclass?
end

prints "true", which is wrong.

hmm. so how else to do it?

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top