Hi --

On Fri, 28 Nov 2008, Daniel Luz wrote:

> Isn't the current behavior the same as using def self.foo?
>
> For example:
>
> class C
> def foo1
>   def bar
>     :bar
>   end
> end
>
> def foo2
>   def self.bar
>     :bar
>   end
> end
> end
>
> I haven't checked how the first case works internally, but my first
> impression is that foo1 and foo2 would do precisely the same (only the
> second form is somewhat more documented, being particularly common for
> defining class methods).

They're not the same. self.bar is a singleton method on whatever
instance of C just called foo2. The bar in foo1 is an instance method
of C, so any instance of C can call it.

class C
   def a
     def b
       puts "In C#b"
     end
   end

   def x
     def self.y
       puts "In singleton method y"
     end
   end
end

c = C.new
c.a              # create b, using a C instance
d = C.new
d.b              # call b on a different C instance

c.x
c.y              # singleton method of c
d.y              # error: d doesn't have y


David

-- 
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS (Jan 12-15) | ADVANCING WITH RAILS (Jan 19-22) *
Both in Fort Lauderdale, FL      * Co-taught with Patrick Ewing
See http://www.rubypal.com for details
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)