使用Ruby,如何将所有数组值转换为给定类型?
问题描述:
我需要将fixnums转换为字符串.我的解决方案是:
I need to convert fixnums to strings. My solution is:
arr.map {|a| a.to_s}
有更好的方法吗?
答
arr.map(&:to_s)
这在Ruby> = 1.8.7中使用了一个令人讨厌的新功能,即,它等同于您问题中的代码.
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.