ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: merch-redmine@jeremyevans.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91594] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
Date: Tue, 19 Feb 2019 15:15:48 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76856.20190219151546.7fe3467b7f2dd006@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15612.20190219124221@ruby-lang.org

Issue #15612 has been updated by jeremyevans0 (Jeremy Evans).


sawa (Tsuyoshi Sawada) wrote:
> 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"
> ```

This would definitely break existing code.  There is a lot of code that expects to be able to access local variables first assigned inside a begin/end block after the begin/end block.

As blocks already do what you want, why not just:

```ruby
tap do
  foo = "foo"
  foo # => "foo"
end
foo # => NameError
```

```ruby
foo = "bar"
tap do
  foo = "foo"
  foo # => "foo"
end
foo # => "foo"
```

You can substitute another method that yields once for `tap` if you want.

----------------------------------------
Feature #15612: A construct to restrict the scope of local variables
https://bugs.ruby-lang.org/issues/15612#change-76856

* 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/

  parent reply	other threads:[~2019-02-19 15:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15612.20190219124221@ruby-lang.org>
2019-02-19 12:42 ` [ruby-core:91591] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables sawadatsuyoshi
2019-02-19 13:28 ` [ruby-core:91592] " matz
2019-02-19 14:38 ` [ruby-core:91593] " shevegen
2019-02-19 15:15 ` merch-redmine [this message]
2019-02-19 22:39 ` [ruby-core:91596] " duerst
2019-02-20  1:55 ` [ruby-core:91598] " nobu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-76856.20190219151546.7fe3467b7f2dd006@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).