[#91458] [Ruby trunk Feature#4475] default variable name for parameter — matz@...
Issue #4475 has been updated by matz (Yukihiro Matsumoto).
3 messages
2019/02/07
[ruby-core:91593] [Ruby trunk Feature#15612] A construct to restrict the scope of local variables
From:
shevegen@...
Date:
2019-02-19 14:38:35 UTC
List:
ruby-core #91593
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/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>