Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Obtaining a reference to an enclosing class of a nested class

0 views
Skip to first unread message

John Lam

unread,
Aug 30, 2006, 11:59:44 AM8/30/06
to
Here is a nested class definition:

class A
class B
end
end

Now, if I have a reference to class B from somewhere:

def get_reference
A::B
end

reference_b = get_reference

How can I get a reference to the enclosing class A?

I can't do this by symbol lookup since class A (and B for that matter)
are anonymous. Am I going to have to explicitly create a
back-reference to the enclosing class at the time that I create class
B?

Thanks
-John
http://www.iunknown.com

Rick DeNatale

unread,
Aug 30, 2006, 2:09:39 PM8/30/06
to
On 8/30/06, John Lam <drj...@gmail.com> wrote:
> Here is a nested class definition:
>
> class A
> class B
> end
> end
>
> Now, if I have a reference to class B from somewhere:
>
> def get_reference
> A::B
> end
>
> reference_b = get_reference
>
> How can I get a reference to the enclosing class A?

Well in this case, since we know(?) it's a Class or Module:

reference_b.name => "A::B"

So you can get A's name by string manipulation.

> I can't do this by symbol lookup since class A (and B for that matter)
> are anonymous. Am I going to have to explicitly create a
> back-reference to the enclosing class at the time that I create class
> B?

Do you really mean that they are anonymous, or that you don't directly
know the name?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

John Lam

unread,
Aug 30, 2006, 2:25:09 PM8/30/06
to
Yep - they're anonymous. I'm rewriting how RubyCLR deals with shadow
class references to generic types, and I've made a design decision
that shadow classes don't really need names. The anonymous class
objects are bound to constants only when necessary (I could avoid this
too, but I haven't measured how expensive calls to const_missing vs. a
hit on a constant are - I suspect it's faster to have constants
pre-created).

I used to mangle class names so that:

List<String>

becomes

List_generic_System_String

but this becomes insanely complex when you consider all of the corner
cases involving generic types and nested generic types. I cut a ton of
complexity out of the code by simply not naming the classes anymore.
That works until this case.

Right now I'm explicitly adding a back-reference to the anonymous
class objects that I create which works just fine. I was just
wondering if there was something in the Ruby type system that would
let me do this already so that I'm not wasting memory duplicating
something that I can already do.

I'm fairly certain that I can't do this, but I thought I'd run it past
folks with more experience than me.

Cheers,
-John
http://www.iunknown.com

Joel VanderWerf

unread,
Aug 30, 2006, 4:20:14 PM8/30/06
to

class A
class B
@nesting = Module.nesting
class << self; attr_reader :nesting; end
end
end

p A::B.nesting # ==> [A::B, A]

However, you say the classes are anonymous. How are you getting B under
the scope of A in that case?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Joel VanderWerf

unread,
Aug 30, 2006, 4:22:00 PM8/30/06
to

class A

Logan Capaldo

unread,
Aug 30, 2006, 4:34:39 PM8/30/06
to

On Aug 30, 2006, at 4:22 PM, Joel VanderWerf wrote:

>
> However, you say the classes are anonymous. How are you getting B
> under
> the scope of A in that case?
>

mod1 = Module.new
mod2 = nil
mod1.module_eval{ mod2 = Module.new }

Joel VanderWerf

unread,
Aug 30, 2006, 6:10:25 PM8/30/06
to
Logan Capaldo wrote:
>
> On Aug 30, 2006, at 4:22 PM, Joel VanderWerf wrote:
>
>>
>> However, you say the classes are anonymous. How are you getting B under
>> the scope of A in that case?
>>
>
> mod1 = Module.new
> mod2 = nil
> mod1.module_eval{ mod2 = Module.new }

I don't think mod2 is nested in mod1, in any sense. There's no reason
for ruby to keep track of the modules that are created during the
execution of the block that accompanies the mod1.module_eval call and to
associate them with mod1. The only reason B is associated with A in

module A
module B; end
end

is that they are lexically related. (More precisely the constant "B" is
in the namespace belonging to the class A.)

Maybe someone else has a more definitive answer?

Logan Capaldo

unread,
Aug 30, 2006, 6:41:21 PM8/30/06
to

VALUE rb_define_module_under(VALUE outer, const char *name)

You could use this to define a named module under an anonymous module.

Rick DeNatale

unread,
Aug 30, 2006, 7:26:45 PM8/30/06
to
But module nesting is namespace nesting, so what does it mean to put a
nameless object in a namespace?


--
Rick DeNatale

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

Joel VanderWerf

unread,
Aug 30, 2006, 7:40:22 PM8/30/06
to
Rick DeNatale wrote:
> But module nesting is namespace nesting, so what does it mean to put a
> nameless object in a namespace?

It's the other way around: a named object in an unnamed namespace, using
rb_define_module_under.

Rick DeNatale

unread,
Aug 31, 2006, 8:26:16 AM8/31/06
to
On 8/30/06, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> Rick DeNatale wrote:
> > But module nesting is namespace nesting, so what does it mean to put a
> > nameless object in a namespace?
>
> It's the other way around: a named object in an unnamed namespace, using
> rb_define_module_under.

Except that the OP said that BOTH were anonymous. Pasting from the
original post:

> I can't do this by symbol lookup since class A (and B for that matter)
> are anonymous.

--

Logan Capaldo

unread,
Aug 31, 2006, 12:25:58 PM8/31/06
to

On Aug 31, 2006, at 8:26 AM, Rick DeNatale wrote:

> On 8/30/06, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
>> Rick DeNatale wrote:
>> > But module nesting is namespace nesting, so what does it mean to
>> put a
>> > nameless object in a namespace?
>>
>> It's the other way around: a named object in an unnamed namespace,
>> using
>> rb_define_module_under.
>
> Except that the OP said that BOTH were anonymous. Pasting from the
> original post:
>
>> I can't do this by symbol lookup since class A (and B for that
>> matter)
>> are anonymous.
>

I was just pointing out ways something like that could possibly come
to occur.

Rick DeNatale

unread,
Aug 31, 2006, 2:00:53 PM8/31/06
to
On 8/31/06, Logan Capaldo <loganc...@gmail.com> wrote:
>
> On Aug 31, 2006, at 8:26 AM, Rick DeNatale wrote:
>
> > On 8/30/06, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> >> Rick DeNatale wrote:
> >> > But module nesting is namespace nesting, so what does it mean to
> >> put a
> >> > nameless object in a namespace?
> >>
> >> It's the other way around: a named object in an unnamed namespace,
> >> using
> >> rb_define_module_under.
> >
> > Except that the OP said that BOTH were anonymous. Pasting from the
> > original post:
> >
> >> I can't do this by symbol lookup since class A (and B for that
> >> matter)
> >> are anonymous.
> >
> I was just pointing out ways something like that could possibly come
> to occur.

No problem. I'm just trying to figure out what he want's to accomplish.

0 new messages