# Writing a plugin: Need to get including class's name. Help?

**URL:** <https://www.ruby-forum.com/t/writing-a-plugin-need-to-get-including-classs-name-help/166633>\
**Category:** Rails\
**Created:** [April 28, 2009, 1:23am UTC](https://www.ruby-forum.com/t/writing-a-plugin-need-to-get-including-classs-name-help/166633 "2009-04-28T01:23:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [April 28, 2009, 1:23am UTC](https://www.ruby-forum.com/t/writing-a-plugin-need-to-get-including-classs-name-help/166633/1 "2009-04-28T01:23:12Z")

</div>

Hello.

For reference, here is some sample code that exemplifies what I am  
trying to do:

> <https://gist.github.com/anonymous/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

---

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [April 28, 2009, 2:09am UTC](https://www.ruby-forum.com/t/writing-a-plugin-need-to-get-including-classs-name-help/166633/2 "2009-04-28T02:09:37Z")

</div>

Chris J. wrote:

> Hello.
> 
> For reference, here is some sample code that exemplifies what I am  
> trying to do:  
> [gist:102816 · GitHub](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:

> <https://gist.github.com/anonymous/102836>

Maybe there was a simpler way(?)

-Chris

---

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [April 28, 2009, 9:04am UTC](https://www.ruby-forum.com/t/writing-a-plugin-need-to-get-including-classs-name-help/166633/3 "2009-04-28T09:04:15Z")

</div>

On Apr 28, 12:23 am, Chris J. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [April 29, 2009, 12:36am UTC](https://www.ruby-forum.com/t/writing-a-plugin-need-to-get-including-classs-name-help/166633/4 "2009-04-29T00:36:34Z")

</div>

Frederick C. wrote:

> On Apr 28, 12:23ï¿½am, Chris J. [removed\_email\_address@domain.invalid](mailto: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
