From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (smtp.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id E35AE1B20053 for ; Wed, 16 Nov 2016 23:36:42 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 3FE31B5D895 for ; Thu, 17 Nov 2016 00:07:02 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id E02A518CC818 for ; Thu, 17 Nov 2016 00:07:02 +0900 (JST) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id ECD6D1204F5; Thu, 17 Nov 2016 00:07:01 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 573E2120493 for ; Thu, 17 Nov 2016 00:06:58 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=1w8qbbqq+vc0lA5vVkzCz5H8t4U=; b=o2egYd9ZbcTuZiWUMF W8FrPIB34VLDTAP8OCSB2/6aNtbNYAyNww/gYlAfupmKceuFnflr+/6/HsREd0/N FIObjOodc2Mu1PVYUbrJfoW5P5KhvxowS/ytmh5OceaKwHD3/i7S7aViqDHheDEr RhkMyMfc7TYg7jICa72Ss+0K4= Received: by filter0592p1mdw1.sendgrid.net with SMTP id filter0592p1mdw1-5788-582C760C-A 2016-11-16 15:06:52.160690871 +0000 UTC Received: from herokuapp.com (ec2-54-146-161-37.compute-1.amazonaws.com [54.146.161.37]) by ismtpd0001p1iad1.sendgrid.net (SG) with ESMTP id kejP3hfVQpS-cGl86Qr3YQ Wed, 16 Nov 2016 15:06:52.031 +0000 (UTC) Date: Wed, 16 Nov 2016 15:06:52 +0000 From: zotherstupidguy@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 53061 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11665 X-Redmine-Issue-Author: keithrbennett X-Redmine-Sender: zotherstupidguy X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5iAJMUc/p5prMnPLbWfoFIolM16iJwEWy81M jsqpun19EuV/PmXpo/yDPu0IEqFkQoUp7PP+jdSRWsgah487xws23aiKi3lXiu225SEWFQX3f76AFq HWqfwuRdVoO1zc1eKPd8lTV8qlW4QbhOHO7KnRg1hNRrghrsqQeDNU8YNQ== X-ML-Name: ruby-core X-Mail-Count: 78175 Subject: [ruby-core:78175] [Ruby trunk Feature#11665] Support nested functions for better code organization X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11665 has been updated by mohamed fouad. Yukihiro Matsumoto wrote: > But at least, the current behavior of nested method definition is useless. It should be made obsolete to open up the future possibility (I'd vote for warning). ~~~ ruby # nested methods allow enforcing dsl constructs scopes def a &block p "a" def b &block p "b" def c &block p "c" end instance_eval &block undef :c end instance_eval &block undef :b end # Works a do b do c do end end end # Doesn't Work b do end c do end ~~~ source: https://gist.github.com/zotherstupidguy/71e45dc0cb1de7e3eb38a89931c808cf ---------------------------------------- Feature #11665: Support nested functions for better code organization https://bugs.ruby-lang.org/issues/11665#change-61548 * 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 (method_count * (method_count) - 1). 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 (6 * (6 - 1)), 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/