开发者

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

开发者 https://www.devze.com 2023-01-28 10:37 出处:网络
I ne开发者_如何学Goed to convert fixnums to strings. My solution is: arr.map {|a| a.to_s} Is there a better way?arr.map(&:to_s)

I ne开发者_如何学Goed to convert fixnums to strings. My solution is:

arr.map {|a| a.to_s}

Is there a better way?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消