# Fun with singletons' singleton classes

**URL:** <https://rubytalk.org/t/fun-with-singletons-singleton-classes/4504>\
**Category:** ruby-talk\
**Created:** [3 February 2003 15:51 UTC](https://rubytalk.org/t/fun-with-singletons-singleton-classes/4504 "2003-02-03T15:51:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [3 February 2003 15:51 UTC](https://rubytalk.org/t/fun-with-singletons-singleton-classes/4504/1 "2003-02-03T15:51:57Z")

</div>

Abusing the language:

batsman@tux-chan:/tmp$ expand -t 2 g.rb  
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  
batsman@tux-chan:/tmp$ ruby g.rb  
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:

> > 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
> 
> Anyone want the new supermount? 🙂  
> whats new aboutit  
> klogd: It cleans whiter than white. 🙂  
> – Seen on #Linux

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [3 February 2003 16:05 UTC](https://rubytalk.org/t/fun-with-singletons-singleton-classes/4504/2 "2003-02-03T16:05:50Z")

</div>

> class A

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.singleton\_method\_added id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Adding singleton method #{id} to #{self}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

> &nbsp;&nbsp;def self.method\_added id  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Adding method #{id} to #{self}"  
> &nbsp;&nbsp;end

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [3 February 2003 17:12 UTC](https://rubytalk.org/t/fun-with-singletons-singleton-classes/4504/3 "2003-02-03T17:12:00Z")

</div>

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.

> **···**
>
> On Tue, Feb 04, 2003 at 01:05:50AM +0900, ts wrote:
> 
> > > class A
> > 
> > ```
> > def self.singleton_method_added id
> > puts "Adding singleton method #{id} to #{self}"
> > end
> > 
> > ```
> > 
> > > def self.method\_added id  
> > > puts “Adding method #{id} to #{self}”  
> > > end
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > 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

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [3 February 2003 18:06 UTC](https://rubytalk.org/t/fun-with-singletons-singleton-classes/4504/4 "2003-02-03T18:06:45Z")

</div>

> How do I do within singleton\_method\_added to remove a singleton method  
> if it matches some :name?

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

class A  
&nbsp;&nbsp;def self.a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "A::a"  
&nbsp;&nbsp;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
