# Singleton class in Ruby

**URL:** <https://www.ruby-forum.com/t/singleton-class-in-ruby/190695>\
**Category:** Ruby\
**Created:** [June 25, 2010, 6:11am UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695 "2010-06-25T06:11:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 25, 2010, 6:11am UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/1 "2010-06-25T06:11:33Z")

</div>

Can we see the singleton class as object in ruby.

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 25, 2010, 9:19am UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/2 "2010-06-25T09:19:16Z")

</div>

On Fri, Jun 25, 2010 at 6:11 AM, Manoj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Can we see the singleton class as object in ruby.

Like this?

irb(main):001:0\> o = Object.new  
=\> #Object:0xb743f3fc  
irb(main):002:0\> singleton = class \<\< o; self; end  
=\> #\<Class:#Object:0xb743f3fc\>  
irb(main):003:0\> singleton  
=\> #\<Class:#Object:0xb743f3fc\>  
irb(main):004:0\> def o.test; “test”; end  
=\> nil  
irb(main):008:0\> singleton.instance\_methods(false)  
=\> [“test”]

Jesus.

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 27, 2010, 3:38pm UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/3 "2010-06-27T15:38:34Z")

</div>

Hi Jesus,

Thanks for your reply.

class Sample

def in\_class  
‘in\_class’  
end

end

@sample = Sample.new

@sample.instance\_eval do

def in\_singleton  
‘in singleton’  
end

end

In @sample object, in\_class method will be available in Sample class.

But in\_singleton method will be available in sinlgeton class of @sample.

That singleton class will be available in memory.

But i couldn’t see that class in the form of object or some thing else.

Thanks,

Manoj

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 27, 2010, 3:43pm UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/4 "2010-06-27T15:43:30Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Fri, Jun 25, 2010 at 6:11 AM, Manoj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Can we see the singleton class as object in ruby.
> 
> Like this?
> 
> irb(main):001:0\> o = Object.new  
> =\> #Object:0xb743f3fc  
> irb(main):002:0\> singleton = class \<\< o; self; end  
> =\> #\<Class:#Object:0xb743f3fc\>  
> irb(main):003:0\> singleton  
> =\> #\<Class:#Object:0xb743f3fc\>  
> irb(main):004:0\> def o.test; “test”; end  
> =\> nil  
> irb(main):008:0\> singleton.instance\_methods(false)  
> =\> [“test”]
> 
> Jesus.

As you said class \<\< o; self; end

In Rails, there is a method called metaclass which is defined in Object

def metaclass  
class \<\< self  
self  
end  
end

is #\<Class:#Object:0xb743f3fc\> the singleton class object for object  
o?

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 27, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/5 "2010-06-27T15:57:18Z")

</div>

On 27 June 2010 16:43, Manoj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> As you said class \<\< o; self; end
> 
> In Rails, there is a method called metaclass which is defined in Object
> 
> def metaclass  
> class \<\< self  
> self  
> end  
> end  
> Yes, and this method is now deprecated because of the wrong name.  
> The chosen name is … singleton\_class 😉  
> So, the answer, in Ruby 1.9 is
> 
> > “mystr”.singleton\_class  
> > =\> #\<Class:#String:0x00000100864fd8\>

> is #\<Class:#Object:0xb743f3fc\> the singleton class object for object  
> o?  
> yes.

B.D.

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 28, 2010, 7:01am UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/6 "2010-06-28T07:01:40Z")

</div>

Benoit D. wrote:

> On 27 June 2010 16:43, Manoj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > As you said ï¿½ class \<\< o; self; end
> > 
> > In Rails, there is a method called metaclass which is defined in Object
> > 
> > def metaclass  
> > ï¿½class \<\< self  
> > ï¿½ ï¿½self  
> > ï¿½end  
> > end  
> > Yes, and this method is now deprecated because of the wrong name.  
> > The chosen name is … singleton\_class 😉  
> > So, the answer, in Ruby 1.9 is
> > 
> > > “mystr”.singleton\_class  
> > > =\> #\<Class:#String:0x00000100864fd8\>
> 
> > is #\<Class:#Object:0xb743f3fc\> the singleton class object for object  
> > o?  
> > yes.
> 
> B.D.

Thanks Benoit.

If i try,

singleton.instance\_methods(false), it is returning [“test”].

But, ‘test’ method is not available in singleton.methods. But it is  
available in singleton.instance\_methods.

Why it is like this? Any special reason for this?

Thanks,  
Manoj

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 28, 2010, 8:57am UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/7 "2010-06-28T08:57:36Z")

</div>

On Mon, Jun 28, 2010 at 7:01 AM, Manoj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > end  
> > > B.D.  
> > > available in singleton.instance\_methods.
> 
> Why it is like this? Any special reason for this?

Because methods returns the list of methods that are accessible for  
the object. In the case of the singleton class, it’s the instance of  
the class (the object) for which you call the method “test”. It’s the  
same thing as with regular classes:

irb(main):001:0\> class A  
irb(main):002:1\> def test  
irb(main):003:2\> “test”  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> A.methods.grep(/test/)  
=\> []  
irb(main):007:0\> A.instance\_methods.grep(/test/)  
=\> [“test”]  
irb(main):008:0\> a = A.new  
=\> #\<A:0xb74d2274\>  
irb(main):009:0\> a.methods.grep(/test/)  
=\> [“test”]

Jesus.

---

<div class="post-metadata">

**Author:** ![manoj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@manoj](https://www.ruby-forum.com/u/manoj)\
**Post date:** [June 29, 2010, 3:58am UTC](https://www.ruby-forum.com/t/singleton-class-in-ruby/190695/8 "2010-06-29T03:58:14Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Mon, Jun 28, 2010 at 7:01 AM, Manoj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > > end  
> > > > B.D.  
> > > > available in singleton.instance\_methods.
> > 
> > Why it is like this? Any special reason for this?
> 
> Because methods returns the list of methods that are accessible for  
> the object. In the case of the singleton class, it’s the instance of  
> the class (the object) for which you call the method “test”. It’s the  
> same thing as with regular classes:
> 
> irb(main):001:0\> class A  
> irb(main):002:1\> def test  
> irb(main):003:2\> “test”  
> irb(main):004:2\> end  
> irb(main):005:1\> end  
> =\> nil  
> irb(main):006:0\> A.methods.grep(/test/)  
> =\> []  
> irb(main):007:0\> A.instance\_methods.grep(/test/)  
> =\> [“test”]  
> irb(main):008:0\> a = A.new  
> =\> #\<A:0xb74d2274\>  
> irb(main):009:0\> a.methods.grep(/test/)  
> =\> [“test”]
> 
> Jesus.

Thanks Jesus,

manoj
