From: Manoj Kumar on 28 Jun 2010 01:01 Benoit Daloze wrote: > On 27 June 2010 16:43, Manoj Kumar <manojs.nitt(a)gmail.com> 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 -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 28 Jun 2010 02:57 On Mon, Jun 28, 2010 at 7:01 AM, Manoj Kumar <manojs.nitt(a)gmail.com> wrote: > Benoit Daloze wrote: >> On 27 June 2010 16:43, Manoj Kumar <manojs.nitt(a)gmail.com> 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? 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.
From: Manoj Kumar on 28 Jun 2010 21:58
Jesús Gabriel y Galán wrote: > On Mon, Jun 28, 2010 at 7:01 AM, Manoj Kumar <manojs.nitt(a)gmail.com> > 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 -- Posted via http://www.ruby-forum.com/. |