From: Ara.T.Howard@... Date: 2005-05-06T03:54:27+09:00 Subject: [SOLVED?] Re: Class#singleton_of --8323328-1942771401-1115317822=:30186 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1942771401-1115317822=:30186" 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-1942771401-1115317822=:30186 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: > >> the challenge is obviously to implement Class#singleton_of without >> parsing the >> output of inspect or, at least so that it works for all cases. > > So are you okay with using Ruby-DL? This is something that ought to be > reasonably easy with evil-ruby. it came to me after only three beers late last night - and no evil-ruby! : (note - i decided 'singleton_super' was a better name than 'singleton_of') jib:~/eg/ruby > cat b.rb module Kernel alias __singleton_method_added singleton_method_added def singleton_method_added(*a, &b) obj =3D self obj.singleton_class.class_eval{ @singleton_super =3D obj } __singleton_method_added(*a, &b) end end class Object def singleton_class @singleton_class ||=3D class << self;self;end end end class Class def singleton_super class_eval{ def ________singleton_super_init;end } unless defined? @= singleton_super @singleton_super end end class A p singleton_super #=3D> nil p singleton_class.singleton_super #=3D> A class << self p singleton_super #=3D> A class << self p singleton_super #=3D> # end end end class C < A p singleton_super #=3D> nil p singleton_class.singleton_super #=3D> C class << self p singleton_super #=3D> C class << self p singleton_super #=3D> # end end end class << [42] p singleton_super #=3D> [42] p singleton_class.singleton_super #=3D> #> end class << Class::new p singleton_super #=3D> # end class << Class::new(Class::new) p singleton_super #=3D> # end p singleton_class.singleton_super #=3D> #>> jib:~/eg/ruby > ruby b.rb nil A A # nil C C # [42] #> # # #>> can anyone break it? 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-1942771401-1115317822=:30186-- --8323328-1942771401-1115317822=:30186--