Issue #4830 has been updated by Jan Lelis.


There is (almost) a case where it is already possible:

"string".gsub(/./){ |e|
  # use e
}

vs.

"string".gsub(/./){
  # use $&
}

I always (except in code-golfing) end up with the first solution. However, when using subgroups, I just love to use them:

"string".gsub(/com(plex)_reg(ex)/){
  # use $1, $2, ...
}

To get back to the original problem, I'd prefer: $item (which is -of course- not a global variable).

PS: Since $dollar variables are rarely used anyway, lets transform them all into special variables :D
----------------------------------------
Feature #4830: Provide Default Variables for Array#each and other iterators
http://redmine.ruby-lang.org/issues/4830

Author: Lazaridis Ilias
Status: Assigned
Priority: Low
Assignee: Yukihiro Matsumoto
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