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
I used to mangle class names so that:
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.
On 8/30/06, Rick DeNatale <firstname.lastname@example.org> wrote:
On 8/30/06, John Lam <email@example.com> wrote:
> Here is a nested class definition:
> class A
> class B
> Now, if I have a reference to class B from somewhere:
> def get_reference
> 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
Do you really mean that they are anonymous, or that you don't directly
know the name?
My blog on Ruby