From: Ara.T.Howard@... Date: 2005-04-29T23:24:33+09:00 Subject: Re: determining when inside 'class << self' On Fri, 28 Apr 2005, Assaph Mehr wrote: > Would you settle for testing if you're inside an anonymous class? Anonymous > classes have names that look like: # > > So you can define your method as: > > def inside_metaclass? > Integer === (self.to_s =~ /^# end > > Which will evaluate to true for anonymous classes. Since (I believe) all > meta-classes are anonymous that can give you a pretty good guess (or at > least has the same result as your sample). > > Out of curiosity I've run: > ObjectSpace.each_object { |o| p [o, o.class] if o.to_s =~ /^# > Which returned ~30 such objects (anonymous classes, unless I'm mistaken). So > unless you're opening an anonymous class, you should be OK (and I'm not even > sure you can reopen an anonymous class). hmm. that will probably work. what i want is something for my attributes module that does class C attribute 'foobar' #=> defines instance method class_attribute 'foobar' #=> defines class instance method class << self attribute 'foobar' #=> defines class instance method end end eg. i want the metacode generater 'attribute' to be context sensitive as in def attribute name if inside_metaclass? and self.is_a? Class class_attribute name else ... end end the above should work but may give unforseen problems... any spring to mind? cheers. -a -- =============================================================================== | 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 accepting | that they pass away. --aitken roshi ===============================================================================