Project

General

Profile

Actions

Feature #4830

closed

Provide Default Variables for Array#each and other iterators

Added by lazaridis.com (Lazaridis Ilias) almost 13 years ago. Updated about 6 years ago.

Status:
Rejected
Target version:
-
[ruby-core:36750]

Description

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0