Mika H.
Mika H.

Reputation: 4329

Ruby array into method

I have an array in Ruby, like ["dog", "cat", bat"]. How can I get an array in which each element is passed to a particular method? For example, if the method reverses the input string, then I expect to get ["god", "tac", "tab"]. I know it's easy to iterate over the elements, pass them into the method, and put the results into an array. But is there a shorter way?

EDIT: I'd also not like to modify the original array.

Upvotes: 1

Views: 94

Answers (1)

David Grayson
David Grayson

Reputation: 87406

["dog", "cat", "bat"].map { |word| word.reverse }

or

["dog", "cat", "bat"].map &:reverse

Upvotes: 6

Related Questions