Project

General

Profile

Actions

Feature #15612

closed

A construct to restrict the scope of local variables

Added by sawa (Tsuyoshi Sawada) about 5 years ago. Updated about 5 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:91591]

Description

We sometimes have local variables that are to be used only to keep track of some temporal states/values during a short routine:

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

begin
  foo = "foo"
  foo # => "foo"
end
foo # => `nil`, or "Undefined local variable or method error"
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?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0