Chris Johnson
2009-Apr-27 23:23 UTC
Writing a plugin: Need to get including class''s name. Help?
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 -- Posted via http://www.ruby-forum.com/.
Chris Johnson
2009-Apr-28 00:09 UTC
Re: Writing a plugin: Need to get including class''s name. Help?
Chris Johnson 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. > ChrisFor 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 -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Apr-28 07:03 UTC
Re: Writing a plugin: Need to get including class''s name. Help?
On Apr 28, 12:23 am, Chris Johnson <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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". >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> Any suggestions here? > > Thanks. > Chris > -- > Posted viahttp://www.ruby-forum.com/.
Chris Johnson
2009-Apr-28 22:36 UTC
Re: Writing a plugin: Need to get including class''s name. Help?
Frederick Cheung wrote:> On Apr 28, 12:23�am, Chris Johnson <rails-mailing-l...@andreas-s.net> > 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. > > FredAhh. Understood. Thanks for clarifying Fred. -Chris -- Posted via http://www.ruby-forum.com/.