Fun with singletons' singleton classes 
Author Message
 Fun with singletons' singleton classes

Abusing the language:


class A
  def self.method_added id
    puts "Adding method #{id} to #{self}"
  end
end

class A
  def a
  end
end

class A
  p self.id
  class << self
    p self.id
    def self.method_added id
      p self.id
      puts "Adding singleton method #{id} to #{self}"
    end
  end
end

class A
  def self.a
  end
end

Adding method a to A
538076252
538072582

I expected
Adding method a to A
538076252
538072582
XXXXXXXXX
Adding singleton method a to Class

Why is method_added not honored for singletons? Note it is perfectly
legal to define singletons' singleton methods:

Quote:
>> class A
>>   class << self
>>     def self.foo
>>       puts "foo"
>>     end
>>   end
>> end
=> nil
>> class << A; foo; end

foo
=> nil

POLS would mean for me that singletons' singletons should behave as "normal"
singletons or be completely unavailable.

--
 _           _                            
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
        Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

<sct> Anyone want the new supermount? :)
<klogd> whats new aboutit
<sct> klogd: It cleans whiter than white. :)
        -- Seen on #Linux



Fri, 22 Jul 2005 23:51:57 GMT  
 Fun with singletons' singleton classes

Quote:
>>>>> "M" == Mauricio =?iso-8859-1?Q?Fern=E1ndez?= <Mauricio> writes:

M> class A

      def self.singleton_method_added id
         puts "Adding singleton method #{id} to #{self}"
      end

M>   def self.method_added id
M>     puts "Adding method #{id} to #{self}"
M>   end

Guy Decoux



Sat, 23 Jul 2005 00:05:50 GMT  
 Fun with singletons' singleton classes

Quote:

> >>>>> "M" == Mauricio =?iso-8859-1?Q?Fern=E1ndez?= <Mauricio> writes:

> M> class A

>       def self.singleton_method_added id
>          puts "Adding singleton method #{id} to #{self}"
>       end

> M>   def self.method_added id
> M>     puts "Adding method #{id} to #{self}"
> M>   end

How do I do within singleton_method_added to remove a singleton method
if it matches some :name?

I tried

class << self
  def singleton_method_added i
    remove_method i if i == :method_added
  end
end

and (more sensible)

def singleton_method_added i
  (class << self; self; end).instance_eval <<-EOF
    remove_method :#{i.to_s} if :#{i.to_s} == :method_added
   EOF
end

I want to render impossible to redefine SomeClass.method_added without
freezing the singleton of SomeClass.

--
 _           _                            
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
        Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

The only other people who might benefit from Linux8086 would be owners
of PDP/11's and other roomsized computers from the same era.
        -- Alan Cox



Sat, 23 Jul 2005 01:12:00 GMT  
 Fun with singletons' singleton classes

Quote:
>>>>> "M" == Mauricio =?iso-8859-1?Q?Fern=E1ndez?= <Mauricio> writes:

M> How do I do within singleton_method_added to remove a singleton method
M> if it matches some :name?

pigeon% cat b.rb
#!/usr/bin/ruby
class A
  def self.singleton_method_added(id)
     puts "Adding singleton method #{id} to #{self}"
     if id != :singleton_method_added
        class << self; self; end.send(:remove_method, id)
     end
  end
end

class A
  def self.a
     p "A::a"
  end
end

A.a
pigeon%

pigeon% b.rb
Adding singleton method singleton_method_added to A
Adding singleton method a to A
/b.rb:17: undefined method `a' for A:Class (NameError)
pigeon%

Guy Decoux



Sat, 23 Jul 2005 02:06:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. 'Singleton' and Null Limited Types

2. singletons and sealed classes

3. Singleton Classes & thread safety

4. creating singleton class from C++

5. Anonymous Classes Vs. Singleton Methods

6. Scope of Singleton Classes

7. Singleton method == Class method ??

8. how to remove method from singleton class

9. singleton classes?

10. Singleton classes (with no such thing as static variables)

11. Singleton class: what is the purpose?

12. KGL's fuzzy values (was: empty and singleton tuples)

 

 
Powered by phpBB® Forum Software