Project

General

Profile

Feature #7432

Explicit way to define local variable in scope

Added by prijutme4ty (Ilya Vorontsov) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Priority:
Normal
Target version:
[ruby-core:50070]

Description

Imagine code such as this
def test_smth
result = nil
assert_nothing_raised { result = some_slow_calculation }
assert_equal expected_answer, result
end
Line result = nil means nothing more than our intention to simply bind variable result in scope of assertion block with outer local variable. In large methods it can be much heavier to find out this obscured intention.
It may be better to introduce a method (if it can be implemented) or a keyword which simply creates a local variable (probably with nil value just to mark its existence)

define_variable 'x' or (define_variable x if it's a keyword)

History

#1 [ruby-core:50071] Updated by charliesome (Charlie Somerville) over 4 years ago

This should definitely not be a method, however I would welcome a 'local' keyword for this purpose.

#2 [ruby-core:50072] Updated by mame (Yusuke Endoh) over 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)
  • Priority changed from Normal to 3
  • Target version set to Next Major

Assigning to matz, but don't hold your breath; matz has rejected such a explicit variable declaration syntax many times.

--
Yusuke Endoh mame@tsg.ne.jp

#3 [ruby-core:50073] Updated by trans (Thomas Sawyer) over 4 years ago

I have always been curious why there is no dynamic way to create local variables (other then eval).

Eg. x = 10 might be dynamically written:

local :x, 10

Since we can create just about anything else dynamically, it seems like stark omission.

#4 [ruby-core:50076] Updated by matz (Yukihiro Matsumoto) over 4 years ago

  • Status changed from Assigned to Rejected

Since I am sick of 'var' and 'local' in other languages, I don't want to add explicit local variable declaration, that requires a new keyword. Introducing a new keyword may break existing programs.

trans (Thomas Sawyer) Your idea would eliminate many chances to optimize.

Matz.

#5 [ruby-core:50078] Updated by trans (Thomas Sawyer) over 4 years ago

I see why then. Thanks.

Also available in: Atom PDF