Feature #8453

Implement Struct.map

Added by Daniel Lobato Garcia 11 months ago. Updated 11 months ago.

[ruby-core:55169]
Status:Closed
Priority:Low
Assignee:-
Category:-
Target version:-

Description

http://ruby-doc.org/core-2.0/Struct.html does not contain any 'map' like method, I imagine given 'each' and 'select' are available it's probably not too hard to implement.

As always I'm 100% open to contribute so if you deem this necessary just let me know and I'll start writing a patch.

History

#1 Updated by Benoit Daloze 11 months ago

  • Status changed from Open to Closed

Struct instances (that is classes produced by Struct.new) include Enumerable.

Actually Struct itself includes Enumerable (written in the left column on the documentation page)
and Struct.new makes Struct an ancestor of the created class.

Struct.new(:a,:b).new(1,2).map { |i| i*2 } #=> [2, 4]

I wonder why #select is overridden though. Probably performance reasons.

#2 Updated by Benoit Daloze 11 months ago

Eregon (Benoit Daloze) wrote:

I wonder why #select is overridden though. Probably performance reasons.

Actually, it is an historical reason: #select used to have #values_at behavior when given integers.

Also available in: Atom PDF