Forum: Ruby on Rails Writing a plugin: Need to get including class's name. Help?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris J. (Guest)
on 2009-04-28 03:23
Hello.

For reference, here is some sample code that exemplifies what I am
trying to do:
http://gist.github.com/102816

I am trying to dynamically defined a class, whose name is either passed
in as a parameter or based on the including class name.

So if I had something like this:
class Green; acts_as_duck; end

I would expect the generated class to be called "GreenDuck", and to be
defined within the "Green" class.

The problem is that no matter what I do, the dynamically referenced
class name is always just "Class".

Any suggestions here?

Thanks.
Chris
Chris J. (Guest)
on 2009-04-28 04:09
Chris J. wrote:
> Hello.
>
> For reference, here is some sample code that exemplifies what I am
> trying to do:
> http://gist.github.com/102816
>
> I am trying to dynamically defined a class, whose name is either passed
> in as a parameter or based on the including class name.
>
> So if I had something like this:
> class Green; acts_as_duck; end
>
> I would expect the generated class to be called "GreenDuck", and to be
> defined within the "Green" class.
>
> The problem is that no matter what I do, the dynamically referenced
> class name is always just "Class".
>
> Any suggestions here?
>
> Thanks.
> Chris

For the record, I got around this by doing some dynamic eval in the
module eval:

http://gist.github.com/102836

Maybe there was a simpler way(?)

-Chris
Frederick C. (Guest)
on 2009-04-28 11:04
(Received via mailing list)
On Apr 28, 12:23 am, Chris J. <removed_email_address@domain.invalid>
wrote:
>
> I would expect the generated class to be called "GreenDuck", and to be
> defined within the "Green" class.
>
> The problem is that no matter what I do, the dynamically referenced
> class name is always just "Class".
>

Because what you have is a class method, self is the class that
acts_as_duck is being called on, ie self == Green, and so self.name
will be 'Green'. self.class is asking ruby what the class of Green is,
and since classes are of class Class you end up with the result you
had.

Fred
Chris J. (Guest)
on 2009-04-29 02:36
Frederick C. wrote:
> On Apr 28, 12:23�am, Chris J. <removed_email_address@domain.invalid>
> wrote:
>>
>> I would expect the generated class to be called "GreenDuck", and to be
>> defined within the "Green" class.
>>
>> The problem is that no matter what I do, the dynamically referenced
>> class name is always just "Class".
>>
>
> Because what you have is a class method, self is the class that
> acts_as_duck is being called on, ie self == Green, and so self.name
> will be 'Green'. self.class is asking ruby what the class of Green is,
> and since classes are of class Class you end up with the result you
> had.
>
> Fred

Ahh.  Understood.  Thanks for clarifying Fred.

-Chris
This topic is locked and can not be replied to.