[RCR] Object#inside_metaclass?

A

Ara.T.Howard

this is very useful for meta-programming:

jib:~/eg/ruby > cat a.rb
class Object
def inside_metaclass?
klass = Class === self ? self : self.class
obj_meta = class << Object; self; end
klass.ancestors.include? obj_meta
end
end

p inside_metaclass?

class << self
p inside_metaclass?
end

class C
p inside_metaclass?
class << self
p inside_metaclass?
class << self
p inside_metaclass?
end
end
end


jib:~/eg/ruby > ruby a.rb
false
true
false
true
true


i believe the implementaion is correct iff the following statement is true:

all meta-classes descend from the meta-class of Object.

-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

Ilias Lazaridis

Ara.T.Howard said:
this is very useful for meta-programming:

jib:~/eg/ruby > cat a.rb
class Object
def inside_metaclass? [...]

i believe the implementaion is correct iff the following statement is true:

all meta-classes descend from the meta-class of Object.

the statement is false.

The Ruby Language, in it's current implementation, has no MetaClasses:

http://lazaridis.com/case/lang/ruby/index.html

-

Please avoid this amateurish re-definition of terminology.

..
 
M

Martin DeMello

Ara.T.Howard said:
i believe the implementaion is correct iff the following statement is true:

all meta-classes descend from the meta-class of Object.

That's a very neat approach. Much more satisfying than string-scraping.

martin
 
D

David A. Black

this is very useful for meta-programming:

jib:~/eg/ruby > cat a.rb
class Object
def inside_metaclass?
klass = Class === self ? self : self.class
obj_meta = class << Object; self; end
klass.ancestors.include? obj_meta
end
end

p inside_metaclass?

i believe the implementaion is correct iff the following statement is true:

all meta-classes descend from the meta-class of Object.

I fear the terminology issue raises its head here. Is there any
reason not to use the customary "singleton class", pending some
pronouncement from Matz that it's no longer the right term?


David
 
I

Ilias Lazaridis

David said:
On Wed, 4 May 2005, Ara.T.Howard wrote: [...]
all meta-classes descend from the meta-class of Object.

I fear the terminology issue raises its head here. Is there any
reason not to use the customary "singleton class",

"singleton class" has already a meaning within OOP.

like this: "a class which has only one instance"
pending some
pronouncement from Matz that it's no longer the right term?

You don't need a pronouncement from the language-designer.

The community should ensure a quick adoption of the Ruby Language.

concise terminology, which does not ignore the status-quo, is an
essential part of this.

http://lazaridis.com/case/lang/ruby/index.html

..
 
J

Jim Weirich

David A. Black said:
I fear the terminology issue raises its head here. Is there any
reason not to use the customary "singleton class", pending some
pronouncement from Matz that it's no longer the right term?

There is a distinction, although some people blur it. Those things that
are almost metaclasses (but aren't because Ruby doesn't have metaclasses)
are a subset of all singleton classes. As noted, these almost-metaclasses
inherit from the almost-metaclass of Object. These almost-metaclasses are
the singleton classes of Class objects.

All almost-metaclasses are singleton classes. Not all singleton classes
are almost-metaclasses.
 
D

David A. Black

Hi --

David A. Black said:

There is a distinction, although some people blur it.

That's what I mean (in case it wasn't clear): the two terms are not
synonymous, and we're seeing more and more of this blurring, and more
and more use of "metaclass" where Matz and most practitioners have
always said "singleton class". I'm hoping we can steer away from
this.


David
 
M

Mark Hubbart

David A. Black said:

There is a distinction, although some people blur it. Those things that
are almost metaclasses (but aren't because Ruby doesn't have metaclasses)
are a subset of all singleton classes. As noted, these almost-metaclasses
inherit from the almost-metaclass of Object. These almost-metaclasses are
the singleton classes of Class objects.

All almost-metaclasses are singleton classes. Not all singleton classes
are almost-metaclasses.

All the more reason to give them a different name... At this point, I
care very little what they're called; virtual class, idioclass, own
class, whatever. Just give us a nice, non-overlapping name to call
them by. I guess singleton class will have to do until then, though...

With all these different names being thrown around, it feels like an
election year again! :)

cheers,
Mark
 
D

David A. Black

Hi --

All the more reason to give them a different name... At this point, I
care very little what they're called; virtual class, idioclass, own
class, whatever. Just give us a nice, non-overlapping name to call
them by. I guess singleton class will have to do until then, though...

Actually one would not want a completely separate name, for the reason
that the almost-metaclasses really are singleton classes. Anything
generated by:

class << self; self; end

is a singleton class. If 'self' is a Class object, then the resulting
singleton class is also an almost-metaclass (parametaclass?!). But it
doesn't cease to be a singleton class.


David
 
J

Jim Weirich

David A. Black said:
Hi --



That's what I mean (in case it wasn't clear): the two terms are not
synonymous, and we're seeing more and more of this blurring, and more
and more use of "metaclass" where Matz and most practitioners have
always said "singleton class". I'm hoping we can steer away from
this.

Then I wasn't clear either. In answer to your question 'Is there any
reason not to use the customary "singleton class"', the answer is "Yes,
because the original posters statement becomes false if you substitute
singleton class for metaclass".

(1) The statement "all singleton classes descend from the singleton class
of Object" is false.

(2) The statement "all metaclasses descend from the metaclass of Object"
is true

(given that that in this context metaclass refers to those things that are
almost-metaclasses, i.e. singleton classes of classes.[1]).

There is a difference in meaning between the word singleton class and
metaclass. The original poster was using the correct terminology and
substituting the term "singleton class" would be incorrect.

--
-- Jim Weirich (e-mail address removed) http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

[1] I'm trying to sidestep the issue of whether these almost-metaclasses
are TRUE metaclasses. They certainly are very much like metaclasses[2].

[2] Pickaxe II, page 382. "Eventually, Matz weighed in with the
following: [...] * Singleton classes for classes behave just like
Smalltalk's metaclasses"
 
A

Ara.T.Howard

That's a very neat approach. Much more satisfying than string-scraping.

the only problem is that is doesn't work !! ;-(

i had another definition in scope in irb when i tested - so sorry.

i'm back to this now, it's the best i can manange attm:

class Class
def inside_metaclass?
inspect =~ %r/^#<Class:/ ? true : false
end
end

class Object
def inside_metaclass?
self.class.inside_metaclass?
end
end


;-(

i'm waiting for the french to come to the rescue.

-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
===============================================================================
 
C

Christoph

Jim said:
(1) The statement "all singleton classes descend from the singleton class
of Object" is false.

(2) The statement "all metaclasses descend from the metaclass of Object"
is true
Nope - the singleton class of Module is not a meta_class but it
descends from the
singleton class of Object.

/Christoph
 
C

Christoph

Ara.T.Howard said:
this is very useful for meta-programming:

jib:~/eg/ruby > cat a.rb
class Object
def inside_metaclass?
klass = Class === self ? self : self.class
obj_meta = class << Object; self; end
klass.ancestors.include? obj_meta
end
end

p inside_metaclass?

class << self
p inside_metaclass?
end

class C
p inside_metaclass?
class << self
p inside_metaclass?
class << self
p inside_metaclass?
end
end
end


jib:~/eg/ruby > ruby a.rb
false
true
false
true
true


i believe the implementaion is correct iff the following statement is
true:

all meta-classes descend from the meta-class of Object.

That is plain false (at least in my understanding of what
you are trying

class << Module
 
C

Christoph

class Class
def inside_metaclass?
inspect =~ %r/^#<Class:/ ? true : false
end
end

class Object
def inside_metaclass?
self.class.inside_metaclass?
end
end

I see - thats what you call a meta class - actually you
statement seems to be right, just the ancestors implementation
in 1.8 seems to have a bug - try


---
def inside_metaclass?
false
end

class << Object
private
def inside_metaclass?
true
end
end
 
F

Florian Groß

Ara.T.Howard said:
this is very useful for meta-programming:

jib:~/eg/ruby > cat a.rb
class Object
def inside_metaclass?

Why? Any real world samples?
 
A

Ara.T.Howard

I see - thats what you call a meta class - actually you
statement seems to be right, just the ancestors implementation
in 1.8 seems to have a bug - try


---
def inside_metaclass?
false
end

class << Object
private
def inside_metaclass?
true
end
end

hmm - seems like something like that could work alright... but this fails:

jib:~/eg/ruby > cat a.rb
def inside_metaclass?
false
end
class << Object
private
def inside_metaclass?
true
end
end

p inside_metaclass?

class << self
p inside_metaclass?
end

class C
p inside_metaclass?
end

jib:~/eg/ruby > ruby a.rb
false
true
true

it's not easy...

-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

Why? Any real world samples?


class C
class << self
trait 'a' => 'default_value'
end

trait 'a' => 'default_value'
end

the first case is defining a trait (attr) with a default value at the class
scope. obviously the default value can be set immediately. in the second
case we are defining an instance method with a default value. this value
obviously cannot be set now so, instead, the code generated does a lazy lookup
of the default value when the value is read the first time. the defaults are
stored in the class as a class instance variable. it's sort hard to show in a
little code but __logically__ we end up with something like (greatly simplified)

class C

@trait_defaults = {
'class' => { 'a' => 'default_value' },
'instance' => { 'a' => 'default_value' },
}

def C::a
@a = @trait_defaults['class']['a'] unless defined? @a
@a
end

def a
@a = @trait_defaults['instance']['a'] unless defined? @a
@a
end
end

so i need to know, at the point of definition if we're inside a metaclass to
generate the code slightly differently. all the above is for explanation only
and is nothing like real code.

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
===============================================================================
 
J

Jim Weirich

Christoph said:
Nope - the singleton class of Module is not a meta_class but it
descends from the
singleton class of Object.

Module is a class (instances of Module are modules). The singleton class
of module would be a metaclass[1] (a class of a class ... since Module is
a class).
 
D

David A. Black

Hi --

David A. Black said:
That's what I mean (in case it wasn't clear): the two terms are not
synonymous, and we're seeing more and more of this blurring, and more
and more use of "metaclass" where Matz and most practitioners have
always said "singleton class". I'm hoping we can steer away from
this.

Then I wasn't clear either. In answer to your question 'Is there any
reason not to use the customary "singleton class"', the answer is "Yes,
because the original posters statement becomes false if you substitute
singleton class for metaclass".

(1) The statement "all singleton classes descend from the singleton class
of Object" is false.

(2) The statement "all metaclasses descend from the metaclass of Object"
is true

(given that that in this context metaclass refers to those things that are
almost-metaclasses, i.e. singleton classes of classes.[1]).

There is a difference in meaning between the word singleton class and
metaclass. The original poster was using the correct terminology and
substituting the term "singleton class" would be incorrect.

I disagree; I think Ara was using the term "metaclass" to mean
"singleton class". I'm taking my cue from his sample code:

# (top level code)

p inside_metaclass? # false

class << self
p inside_metaclass? # true
end

So his concept of "inside a metaclass" includes "inside a non-class
object's singleton class" (since top-level self is not a class).

He then predicates that his code only works iff "all metaclasses
descend from the metaclass of Object", but if he's using
"metaclass" to mean the same thing he meant it to mean in
"inside_metaclass?", then this means singleton class (and the
statement is false).

(I'm working in the dark a little here as the examples all print
'false' for me in 1.8.2 and slightly old 1.9. I guess Ara's using a
recent 1.9 -- ?)


David
 
D

David A. Black

Hi --
it's sort hard to show in a little code but __logically__ we end up
with something like (greatly simplified)

class C

@trait_defaults = {
'class' => { 'a' => 'default_value' },
'instance' => { 'a' => 'default_value' },
}

def C::a

(Just out of curiosity: do you mean to depart from the usual C.a here?
And why? :)
@a = @trait_defaults['class']['a'] unless defined? @a
@a
end

def a
@a = @trait_defaults['instance']['a'] unless defined? @a
@a
end
end

so i need to know, at the point of definition if we're inside a
metaclass to generate the code slightly differently. all the above
is for explanation only and is nothing like real code.

I understand the not real code point, but still, it's showing
something I can't quite follow, namely the @trait_defaults instance
variable in the instance method definition (def a). Do you mean this
to refer to the class's instance variable?

I'm also not sure where inside_metaclass? would go here. Can you
rewrite it into pseudo-code looking how it would look if that method
existed?


David
 

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

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top