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.0 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=no 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 467211F453 for ; Tue, 19 Feb 2019 12:42:30 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6D4B6120CF8; Tue, 19 Feb 2019 21:42:27 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id DCE84120CF8 for ; Tue, 19 Feb 2019 21:42:24 +0900 (JST) Received: by filter0068p3las1.sendgrid.net with SMTP id filter0068p3las1-22334-5C6BF9AE-10 2019-02-19 12:42:22.387871309 +0000 UTC m=+294023.284481784 Received: from herokuapp.com (ec2-54-227-62-255.compute-1.amazonaws.com [54.227.62.255]) by ismtpd0046p1iad1.sendgrid.net (SG) with ESMTP id tDbDwT3ITeeT2L8jERSvxw for ; Tue, 19 Feb 2019 12:42:22.246 +0000 (UTC) Date: Tue, 19 Feb 2019 12:42:23 +0000 (UTC) From: sawadatsuyoshi@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67042 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15612 X-Redmine-Issue-Author: sawa X-Redmine-Sender: sawa 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS6QtxT8lTsBgooPTiw7ZNSH1Eq9imrKoDuSeO 1Mz7jU4rIHN5EDrhIe34wAkXmDo1QodEUrA8b0LOt62ySw1eNVoAjurmvFnNvFRdb8OM/b9MhrBMlS cHXQPju4HZVxOXriWworu7jhRZLKcP6BQj9DXW4XH+ZYYU32IEHfqpRNzQ== X-ML-Name: ruby-core X-Mail-Count: 91591 Subject: [ruby-core:91591] [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 reported by sawa (Tsuyoshi Sawada). ---------------------------------------- Feature #15612: A construct to restrict the scope of local variables https://bugs.ruby-lang.org/issues/15612 * Author: sawa (Tsuyoshi Sawada) * Status: Open * 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/