From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.6 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 24EC41F453 for ; Wed, 20 Feb 2019 01:55:55 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 06834121A04; Wed, 20 Feb 2019 10:55:51 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id D6BB41219B1 for ; Wed, 20 Feb 2019 10:55:48 +0900 (JST) Received: by filter0109p3las1.sendgrid.net with SMTP id filter0109p3las1-25729-5C6CB3A2-11 2019-02-20 01:55:46.388195947 +0000 UTC m=+3314.232409146 Received: from herokuapp.com (ec2-54-159-238-58.compute-1.amazonaws.com [54.159.238.58]) by ismtpd0010p1iad1.sendgrid.net (SG) with ESMTP id hkbQWYzYT0uP_EjFYe-WSA for ; Wed, 20 Feb 2019 01:55:46.219 +0000 (UTC) Date: Wed, 20 Feb 2019 01:55:47 +0000 (UTC) From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67049 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15612 X-Redmine-Issue-Author: sawa X-Redmine-Sender: nobu 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS5e9BHgjFD8yb2RtJWE83p2TW4ILFVDFXANTN +llQKIqkldpjLEgPMwcjaQJLkNFRFjoit/Phr0q2ptj8Noq6SeUylhWbIXohOLX0Wn5OjYvwjQaNuJ Yg+8pkqRZ0/M3BuJCCq0oWltHeEvaIfNA5xqF/VLCF24B7YV73yfQupXyw== X-ML-Name: ruby-core X-Mail-Count: 91598 Subject: [ruby-core:91598] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables 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 #15612 has been updated by nobu (Nobuyoshi Nakada). jeremyevans0 (Jeremy Evans) wrote: > As blocks already do what you want, why not just: > > ```ruby > foo = "bar" > tap do > foo = "foo" > foo # => "foo" > end > foo # => "foo" > ``` You can "declare" overriding local variables. ```ruby foo = "bar" tap do |;foo| foo = "foo" foo # => "foo" end foo # => "bar" ``` ---------------------------------------- Feature #15612: A construct to restrict the scope of local variables https://bugs.ruby-lang.org/issues/15612#change-76860 * Author: sawa (Tsuyoshi Sawada) * Status: Feedback * Priority: Normal * Assignee: * Target version: ---------------------------------------- We sometimes have local variables that are to be used only to keep track of some temporal states/values during a short routine: ```ruby ... foo = some_initial_value some_routine_that_uses_foo ... ``` Currently, the scope of local variables are either a proc, a block, `loop` body, a method definition, or a class/module definition, but such routines are sometimes just only a part of them. In order to improve readability of the code by explicitly indicating the scope of such local variables, and to avoid pollution by the variable, I propose to have some construct to restrict the scope of local variables. One possibility, without adding a new keyword to the current syntax, is to use the `begin`...`end` construct. The expected behavior would be: ```ruby begin foo = "foo" foo # => "foo" end foo # => `nil`, or "Undefined local variable or method error" ``` ```ruby foo = "bar" begin foo = "foo" foo # => "foo" end foo # => "bar" ``` Or, does this break the existing code too much? If so, can a new construct be added to the current syntax? -- https://bugs.ruby-lang.org/