From: Ara.T.Howard@... Date: 2005-05-05T03:54:56+09:00 Subject: Re: [RCR] Object#inside_metaclass? --8323328-883530348-1115231498=:19717 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-883530348-1115231498=:19717" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-883530348-1115231498=:19717 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Thu, 5 May 2005, [ISO-8859-1] Florian Gro=DF wrote: > Ara.T.Howard wrote: > >> this is very useful for meta-programming: >> >> jib:~/eg/ruby > cat a.rb >> class Object >> def inside_metaclass? > > Why? Any real world samples? class C class << self trait 'a' =3D> 'default_value' end trait 'a' =3D> 'default_value' end the first case is defining a trait (attr) with a default value at the class scope. obviously the default value can be set immediately. in the second case we are defining an instance method with a default value. this value obviously cannot be set now so, instead, the code generated does a lazy loo= kup of the default value when the value is read the first time. the defaults a= re stored in the class as a class instance variable. it's sort hard to show i= n a little code but __logically__ we end up with something like (greatly simpli= fied) class C @trait_defaults =3D { 'class' =3D> { 'a' =3D> 'default_value' }, 'instance' =3D> { 'a' =3D> 'default_value' }, } def C::a @a =3D @trait_defaults['class']['a'] unless defined? @a @a end def a @a =3D @trait_defaults['instance']['a'] unless defined? @a @a end end so i need to know, at the point of definition if we're inside a metaclass t= o generate the code slightly differently. all the above is for explanation o= nly and is nothing like real code. cheers. -a --=20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | renunciation is not getting rid of the things of this world, but acceptin= g | that they pass away. --aitken roshi =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-883530348-1115231498=:19717-- --8323328-883530348-1115231498=:19717--