Issue #4830 has been updated by Lazaridis Ilias.


[please, if possible, delete the non-relevant quoted message in your reply.]

Magnus Holm wrote:
> What happens in this case?
>  
>    item = 1
>    [1, 2, 3].each { print item }

Possibly the same as with

    item = 1
    [1, 2, 3].each { |item| print item }

The outer local variable gets shadowed by the inner one.

But: 

the first step is to determine if such feature is to be included into the language.

If it's decided, then the implementation details, possible problems and tradeoffs become relevant.


----------------------------------------
Feature #4830: Provide Default Variables for Array#each and other iterators
http://redmine.ruby-lang.org/issues/4830

Author: Lazaridis Ilias
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 


for arrays: use "item" by default
for hashes: use "key" and "value" by default

names = ["Jane", "Michele", "Isabella"]
names.each { |name| print name, "\n" } 
names.each { print item, "\n" } 

contact = {name:"Jane", phone:"1234567"}
contact.each { |key, value| print key, ": ", value, "\n"}
contact.each { print key, ": ", value, "\n"}

-

The benefits are:

* more compact code (without loosing clarity of the code).
* no repetitions ("names, name, name") in a one-liner with {} block

This extension does not break any existent behaviour.


-- 
http://redmine.ruby-lang.org