Issue #11665 has been updated by Keith Bennett.


I completely forgot about the notation of defining instance methods within other instance method.

My first reaction to rediscovering this was that it would be useful for me, since it would visually communicate the relationship between the inner and outer methods.  However, that communication would be a fiction, because the inner defined method is just another instance method.  Furthermore, my guess is that defining an instance method every time a method is called is much more expensive than defining a lambda.

So I agree with what I think Matz and Hans are saying (Matz, I probably don't completely understand what you mean by '"real functions" with Java|C++ private scope."' though) -- if we can create a construct that cannot access the binding in which it was defined, and can be created only once and not every time a method is called, then that would be just about as good as nested methods (maybe even better). (As a kludge, one could do something like: @fn_x ||= -> {...} ), but it would be nice not to have to define an instance variable to refer to the local function.)

Yusuke, we could always use define_method for the cases you describe, though I agree with you that it is probably clearer to the reader to use 'def function_name'.

Regarding refinements, I confess that I do not understand them yet, but I'm wondering if something as simple as a context-free function should require a relatively complex construct.

And Dan, regarding the inability to test lambdas (and possibly the new inner functions), I suggest using methods where testing at that level is required, but I believe there are many cases where lambdas can do low level implementation details whose behavior can be adequately tested by testing the enclosing method.  (One can also extract a class for a complex method containing several lambdas, of course.)

-- Keith



----------------------------------------
Feature #11665: Support nested functions for better code organization
https://bugs.ruby-lang.org/issues/11665#change-54853

* Author: Keith Bennett
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
The wisdom of using local variables has been internalized in all of us from the beginning of our software careers.  If we need a variable referring to data that is used only in a single method, we create a local variable for it.

Yet if it is logic to which we need to refer, we make it an instance method instead.

In my opinion, this is inconsistent and unfortunate. The result is a bloated set of instance methods that the reader must wade through to mentally parse the class.  The fact that some of these methods are used only by one other method is never communicated by the code; the reader has to discover that for him/herself.

The number of possible interactions among the instance methods is one of many measures of our software's complexity.  The number of possible instance method interactions is <code>(method_count * (method_count) - 1)</code>.  Using this formula, a class with 10 methods will have a complexity of 90.  If 4 of those methods are used by only 1 other method, and we could move them inside those methods, the complexity would plummet to 30 <code>(6 * (6 - 1))</code>, a third of the original amount!

While it is possible to extract subsets of these methods into new smaller classes, this is not always practical, especially in the case of methods called only by the constructor.

Fortunately, we do have lambdas in Ruby, so I will sometimes create lambdas inside methods for this purpose.  However, lambdas are not as isolated as methods, in that they can access and modify local variables previously defined outside their scope.  Furthermore, the lambdas can be passed elsewhere in the program and modify those locals from afar! So using methods would be cleaner and safer.

Another weakness of using lambdas for this purpose is that, unlike methods that are created at interpret time, lambdas are objects created at runtime -- so if a method creating 2 lambdas is called a million times in a loop, you'll need to create and garbage collect another 2 million objects. (This can be circumvented by defining the lambdas as class constants or assigning them to instance variables, but then they might as well be instance methods.)

I realize that implementing this feature would be a substantial undertaking and may not be feasible at this time. That said, I think it would be useful to discuss this now so we might benefit from its implementation someday.

* * * *

(Much of this content is communicated in my talk on Ruby lambdas; slide show is at https://speakerdeck.com/keithrbennett/ruby-lambdas-functional-conf-bangalore-oct-2014 and YouTube video of the presentation at FunctionalConf in Bangalore at https://www.youtube.com/watch?v=hyRgf6Qc5pw.)

Also, this post is also posted as a blog article at http://www.bbs-software.com/blog/2015/11/07/the-case-for-nested-methods-in-ruby/.



-- 
https://bugs.ruby-lang.org/