From: 7stud -- Date: 2008-01-23T17:13:12+09:00 Subject: Re: singleton definition differences Nicholas Randal wrote: > would someone please explain the difference between these definitions > > class A > class << self > def hello > puts 'hello' > end > end > end > > class B > def B.hello > puts 'hello' > end > end > > A.hello > B.hello According to p. 36, "Programming Ruby(2nd ed.)", they are different syntaxes for accomplishing the same thing. If you define class A like this: class A def sayhi puts "hi" end end a = A.new a.sayhi then you have this situation: a -- instance of class A | | | V class A methods: ---sayhi() | | | V class Object methods: ---send(), etc. If you define class A like you did: > class A > class << self > def hello > puts 'hello' > end > end > end then this is what happens: a -- instance of class A | | | V class A methods: -- | | | V class A' -- a superclass ( or "singleton class") for A only containing A's class methods methods: ---hello() | | | V class Object methods --send(), etc. In effect, you inserted a new class between class A and class Object. The dot notation does the same thing. -- Posted via http://www.ruby-forum.com/.