Project

General

Profile

Actions

Feature #5710

closed

Enumerable#each( :method ) and Enumerable#map( :method )

Added by battox (Matías Battocchia) over 12 years ago. Updated over 12 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:41486]

Description

#each and #map could accept an argument of class string or symbol and send it to the objects in the enumerable.

An example:

array = [ "bi", "tri", "quad" ]
p array.each( :upcase! ) #=> [ "BI", "TRI", "QUAD" ]
p array.map( :length ) #=> [ 2, 3, 4 ]

It is the same --but succinctly-- as:

array.each { | string |
string.upcase!
}

array.map { | string |
string.length
}

This abbreviation is useful for those simple cases when you need to call a method over, or retrieve the values of a property of a set of objects.

Updated by trans (Thomas Sawyer) over 12 years ago

Try

array = [ "bi", "tri", "quad" ]
p array.each( &:upcase! ) #=> [ "BI", "TRI", "QUAD" ]
p array.map( &:length ) #=> [ 2, 3, 4 ]

Updated by battox (Matías Battocchia) over 12 years ago

It works. Thank you!

I guess that this issue has been resolved...

Updated by duerst (Martin Dürst) over 12 years ago

  • Status changed from Open to Closed

closed at request of submitter

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0