herpderp
herpderp

Reputation: 16187

Using Ruby, how do I convert all array values to a given type?

I need to convert fixnums to strings. My solution is:

arr.map {|a| a.to_s}

Is there a better way?

Upvotes: 21

Views: 18363

Answers (1)

Wayne Conrad
Wayne Conrad

Reputation: 108089

arr.map(&:to_s)

This uses a spiffy new feature in Ruby >= 1.8.7, the "symbol to proc" shortcut, and is equivalent to the code in your question.

Upvotes: 44

Related Questions