ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:91591] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
       [not found] <redmine.issue-15612.20190219124221@ruby-lang.org>
@ 2019-02-19 12:42 ` sawadatsuyoshi
  2019-02-19 13:28 ` [ruby-core:91592] " matz
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: sawadatsuyoshi @ 2019-02-19 12:42 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:91592] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
       [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 ` matz
  2019-02-19 14:38 ` [ruby-core:91593] " shevegen
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: matz @ 2019-02-19 13:28 UTC (permalink / raw)
  To: ruby-core

Issue #15612 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Feedback

I am not sure how readability improved by adding explicit scoping. So you have to prove it first.
From my point of view, overriding local variables in the example is a source of confusion, rather than readability.

Matz.


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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:91593] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
       [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 ` shevegen
  2019-02-19 15:15 ` [ruby-core:91594] " merch-redmine
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: shevegen @ 2019-02-19 14:38 UTC (permalink / raw)
  To: ruby-core

Issue #15612 has been updated by shevegen (Robert A. Heiler).


I think this is an interesting idea but the syntax is a bit confusing (to
me).

This may be because I am so used to:

    begin
      code_that_may_break
    rescue Duck
      puts 'all ducklings were rescued.'
    end

It seems a bit surprising to see "begin" used with the intent to
modify/restrict/extend local variables specifically.

Personally I do not need many local variables for most of my methods.
The longer a method becomes, the harder it is to modify it (at the
least when I try to).

It may be better to try another construct than begin/end, if only
for illustration purpose (it may read better with an alternative
to begin/end, but I can not think of a good alternative myself).

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:91594] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
       [not found] <redmine.issue-15612.20190219124221@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-02-19 14:38 ` [ruby-core:91593] " shevegen
@ 2019-02-19 15:15 ` merch-redmine
  2019-02-19 22:39 ` [ruby-core:91596] " duerst
  2019-02-20  1:55 ` [ruby-core:91598] " nobu
  5 siblings, 0 replies; 6+ messages in thread
From: merch-redmine @ 2019-02-19 15:15 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:91596] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
       [not found] <redmine.issue-15612.20190219124221@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-02-19 15:15 ` [ruby-core:91594] " merch-redmine
@ 2019-02-19 22:39 ` duerst
  2019-02-20  1:55 ` [ruby-core:91598] " nobu
  5 siblings, 0 replies; 6+ messages in thread
From: duerst @ 2019-02-19 22:39 UTC (permalink / raw)
  To: ruby-core

Issue #15612 has been updated by duerst (Martin Dürst).


On top of what others have said, methods in Ruby should normally be quite short. If you have a method that's so long that you think you need restricted scopes for local variables, you should look at how to split the code into several methods.

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [ruby-core:91598] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
       [not found] <redmine.issue-15612.20190219124221@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-02-19 22:39 ` [ruby-core:91596] " duerst
@ 2019-02-20  1:55 ` nobu
  5 siblings, 0 replies; 6+ messages in thread
From: nobu @ 2019-02-20  1:55 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-02-20  1:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:91594] " merch-redmine
2019-02-19 22:39 ` [ruby-core:91596] " duerst
2019-02-20  1:55 ` [ruby-core:91598] " nobu

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).