UPDATE. Since this answer seems to have gained some interest recently, I would like to point out that there is a discussion on the tracker issue with the Ruby problem in order to remove the function being discussed here, namely, to prohibit the definition of methods inside the method body .
No, Ruby has no nested methods.
You can do something like this:
class Test1 def meth1 def meth2 puts "Yay" end meth2 end end Test1.new.meth1
But this is not a nested method. I repeat: Ruby has no nested methods.
What it is is the definition of a dynamic method. When you start meth1 , the body of meth1 will be executed. The body simply defines a method called meth2 , so after running meth1 once, you can call meth2 .
But where is meth2 defined? Well, this is not explicitly defined as a nested method, since there are no nested methods in Ruby. It is defined as an instance method of Test1 :
Test1.new.meth2
In addition, it will be redefined each time meth1 started:
Test1.new.meth1 # Yay Test1.new.meth1. # test1.rb:3: warning: method redefined; discarding old meth2 # test1.rb:3: warning: previous definition of meth2 was here # Yay
In short: no, Ruby does not support nested methods.
Also note that in Ruby, method bodies cannot be private, only blocking bodies can. This pretty much eliminates the basic use of nested methods, because even if Ruby supports nested methods, you cannot use external method variables in a nested method.
UPDATE CONTINUED: at a later stage, this syntax could be reused to add nested methods in Ruby that will behave as I described: they will be bound to their containing method, i.e. invisible and inaccessible outside their containing method body. And, perhaps, they will have access to their substantive lexical sphere. However, if you read the discussion I linked above, you may notice that matz is highly dependent on nested methods (but still to remove nested method definitions).