Singleton class in Ruby

J

Jesús Gabriel y Galán

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.
 
M

Manoj Kumar

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
 
M

Manoj Kumar

Jesús Gabriel y Galán said:
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?
 
B

Benoit Daloze

As you said =A0 class << o; self; end

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

def metaclass
=A0class << self
=A0 =A0self
=A0end
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
=3D> # said:
is #<Class:#<Object:0xb743f3fc>> the singleton class object for object
o?
yes.

B.D.
 
M

Manoj Kumar

Benoit said:
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

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
 
J

Jesús Gabriel y Galán

Benoit said:
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

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?

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
=3D> nil
irb(main):006:0> A.methods.grep(/test/)
=3D> []
irb(main):007:0> A.instance_methods.grep(/test/)
=3D> ["test"]
irb(main):008:0> a =3D A.new
=3D> #<A:0xb74d2274>
irb(main):009:0> a.methods.grep(/test/)
=3D> ["test"]

Jesus.
 
M

Manoj Kumar

Jesús Gabriel y Galán said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top