From: Logan Capaldo Date: 2006-04-03T05:44:43+09:00 Subject: Re: Dynamic code generation On Apr 1, 2006, at 11:15 PM, Thiago Arrais wrote: > On 4/1/06, Logan Capaldo wrote: >> I don;t see what you gain by meta-programming here > > I need a method to identify the child classes, something like > Class#name, just more detailed. All instances of subclasses of the > parent class need to respond to that method, but the method returns > the same thing for all instances: a class identifier. Most classes > will respond in mostly the same way, but some will need customization. > > Maybe a (almost) real example will help: > > ---- > class Parent > > def identifier > return "This is class #{self.class.name}" > end > > end > > class ChildUsingDefaultIdentifier; end > > class ChildUsingCustomIdentifier > > def identifier > return 'Instances of this class identify them on a totally > different way' > end > > end > ---- > > Sure we could just override the method in the subclasses that needed > to override it (like we did above), but it is just an identifier and a > whole def block seemed too much. > > Regards, > > Thiago Arrais > This seems like a job for class constants: % cat identifiers.rb class A ID = "Class A" end class B < A # uses default id end class C < A # uses a custom id ID = "Class C" end puts A::ID puts B::ID puts C::ID puts A::ID % ruby identifiers.rb Class A Class A Class C Class A