How can I convert these:
我怎样才能转换这些:
[172592596,93038592,154137572]
To look like these:
看起来像这样:
['172592596','93038592','154137572']
2 个解决方案
#1
11
If you want to turn an array of ints into an array of strings, you can do so easily using map
and to_s
.
如果要将int数组转换为字符串数组,可以使用map和to_s轻松完成。
arr = [172592596,93038592,154137572]
arr.map {|x| x.to_s}
#=> ["172592596", "93038592", "154137572"]
Since this is rails, you can also do (will also work in plain ruby if the version is at least 1.8.7):
由于这是rails,你也可以这样做(如果版本至少为1.8.7,也可以使用普通红宝石):
arr.map(&:to_s)
To get the same result.
得到相同的结果。
#2
0
Try this!
b = []
a = [172592596,93038592,154137572]
a.each {|a1| b << a1.to_s}
b will return ["172592596", "93038592", "154137572"]
You can also use collect! same as map like @sepp2k suggested.
你也可以使用收集!和@ sepp2k建议的地图相同。
a = [172592596,93038592,154137572]
a.collect! {|x| x.to_s}
#1
11
If you want to turn an array of ints into an array of strings, you can do so easily using map
and to_s
.
如果要将int数组转换为字符串数组,可以使用map和to_s轻松完成。
arr = [172592596,93038592,154137572]
arr.map {|x| x.to_s}
#=> ["172592596", "93038592", "154137572"]
Since this is rails, you can also do (will also work in plain ruby if the version is at least 1.8.7):
由于这是rails,你也可以这样做(如果版本至少为1.8.7,也可以使用普通红宝石):
arr.map(&:to_s)
To get the same result.
得到相同的结果。
#2
0
Try this!
b = []
a = [172592596,93038592,154137572]
a.each {|a1| b << a1.to_s}
b will return ["172592596", "93038592", "154137572"]
You can also use collect! same as map like @sepp2k suggested.
你也可以使用收集!和@ sepp2k建议的地图相同。
a = [172592596,93038592,154137572]
a.collect! {|x| x.to_s}