arr
=
"
1,2,3
"
.split(
'
,
'
)
arr.map!{
|
item
|
item
=
item.to_i}
#
一般可以用map!方法来改变原数组内容
0.upto(arr.length
-
1
){
|
idx
|
arr[idx]
=
arr[idx].to_s}
#
再将数组内容改回字符串形式 使用upto方法
fixnum对象的upto和downto方法,可以很方便的作为访问一个数组的索引。
其实也可以这样做
(0..arr.length
-
1
).each{
|
n
|
arr[n]
=
arr[n].to_i}
总之ruby是想怎么写就怎么写 非常方便
本文介绍了一种使用Ruby的Fixnum对象方法upto和downto来遍历并修改数组元素的技术。通过示例展示了如何利用这些方法高效地转换数组内容。
2288

被折叠的 条评论
为什么被折叠?



