Issue #4830 has been updated by Charles Nutter.


I've gone back and forth on whether I like this feature in Groovy. For simple iterations or chained iterations, it definitely shortens things up:

some_array.map { foo(it) }.select { bar(it) }.each { baz(it) }

versus

some_array.map {|it| foo(it) }.select {|it| bar(it) }.each {|it| baz(it) }

<tongue in cheek>

Perhaps something more Scala-like:

some_array.map { foo(_) }.select { bar(_) }.each { baz(_) }

That's actually fairly clean in the normal form and explicitly passing arguments as normal:

some_array.map {|_| foo(_) }.select {|_| bar(_) }.each {|_| baz(_) }

</tongue in cheek>

Yes, it looks like ass. But I think it's better to be explicit than implicit most of the time (anti-magic variable) and better to just use words rather than symbols (anti-special char or $ variable).

Ask me again tomorrow and I might have changed my mind and love the feature.

FWIW, I have implemented "it" in JRuby previously, just for fun. It's not hard to add, if the powers decide it's a good feature for Ruby.
----------------------------------------
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