------art_2670_14895185.1144689566610
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 4/10/06, Gregory Seidman <gsslist+ruby / anthropohedron.net> wrote:
>
> On Mon, Apr 10, 2006 at 11:58:43PM +0900, TRANS wrote:
> } > Is it possible to add class attributes (cattr_accessor) using a
> module?
> } > If so could you give an example?
> }
> } There is no "Ruby Way" to do this b/c Ruby's module methods aren't
> } inherited by classes that include them. Though I've asked for a way
> } man times now!
> [...]
> } Only ugly meta-programming hacks will give the sought behavior --and
> } even then their are corner-case complications.
>
> Does this count as ugly meta-programming?
>
> module M
>   def self.included(klass)
>     class << klass
>       attr_accessor :m
>     end
>   end
> end


It is much prettier than the following ugly meta-programming, but I have no
clue how it works !!!
Could you explain please?
Thx
Robert

} T.
> --Greg




class Module
>         def cattr( *names )
>                 names.map{ |name| name.to_s }.each do
>                         |name|
>                         eval <<-EOEval
>         def self.#{name}; @#{name}; end
>         def self.#{name}=(x); @#{name}=x; end
>                         EOEval
>                         define_method( name ){ self.class.send( name ) }
>                         define_method( name + "="){ |v| self.class.send(
> name + "=", v ) }
>                 end
>         end
> end
>
>


--
Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

------art_2670_14895185.1144689566610--