--8323328-1942771401-11153178220186
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-11153178220186
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Thu, 5 May 2005, [ISO-8859-1] Florian Growrote:

> 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 = self
       obj.singleton_class.class_eval{ @singleton_super = obj }
       __singleton_method_added(*a, &b)
     end
   end

   class Object
     def singleton_class
       @singleton_class ||= 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  #=> nil
     p singleton_class.singleton_super  #=> A
     class << self
       p singleton_super  #=> A
       class << self
         p singleton_super  #=> #<Class:A>
       end
     end
   end

   class C < A
     p singleton_super  #=> nil
     p singleton_class.singleton_super  #=> C
     class << self
       p singleton_super  #=> C
       class << self
         p singleton_super  #=> #<Class:C>
       end
     end
   end

   class << [42]
     p singleton_super  #=> [42]
     p singleton_class.singleton_super  #=> #<Class:#<Array:0xb7436bfc>>
   end

   class << Class::new
     p singleton_super  #=> #<Class:0xb7436aa8>
   end

   class << Class::new(Class::new)
     p singleton_super  #=> #<Class:0xb7436814>
   end

   p singleton_class.singleton_super  #=> #<Object:0xb7447a10 @singleton_class=#<Class:#<Object:0xb7447a10>>>



   jib:~/eg/ruby > ruby b.rb
   nil
   A
   A
   #<Class:A>
   nil
   C
   C
   #<Class:C>
   [42]
   #<Class:#<Array:0xb7436bac>>
   #<Class:0xb7436a58>
   #<Class:0xb74367c4>
   #<Object:0xb7447a10 @singleton_class=#<Class:#<Object:0xb7447a10>>>


can anyone break it?

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
===============================================================================

--8323328-1942771401-11153178220186--
--8323328-1942771401-11153178220186--