使用Ruby,如何将所有数组值转换为给定类型? [英] Using Ruby, how do I convert all array values to a given type?

查看:63
本文介绍了使用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.

这篇关于使用Ruby,如何将所有数组值转换为给定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆