From: Pit Capitain Date: 2004-10-02T00:54:01+09:00 Subject: Re: Can you rewrite this in a better way? Bob Sidebotham schrieb: > I would like to define a method that can be used to compactly define > some items of interest (here called "fields") for subclasses. > > (...) > > Here are two definitions for class A that support these semantics: > > ---#1------------------------------------------------ > > (...) > > ---#2------------------------------------------------ > > class A > def self.fields(*fields) > eval "def fields > [#{fields.collect{|f| ':' + f.to_s}.join(',')}] > end" > end > end > > ----------------------------------------------------- > > Is there a better way (more efficient, shorter and/or clearer) to do this? #2 could be implemented as: class A def self.fields(*fields) define_method(:fields) { fields } end end Here you don't need to build a textual version of the fields array. Regards, Pit