Reputation: 4329
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
Reputation: 87406
["dog", "cat", "bat"].map { |word| word.reverse }
or
["dog", "cat", "bat"].map &:reverse
Upvotes: 6