0

我有以下 3x3 数组:

grid = [["1","2","3"],["4","5","6"],["7","8","9"]]

我想将每个数字从 String 转换为 Fixnum。我试过了:

grid.each{ |thing| thing.each { |subthing| subthing = subthing.to_i }}

但是,puts grid[0][0].class仍然输出String.

4

1 回答 1

2

each只是迭代每个值,它不会改变内容。尝试使用map.

foo = grid.map{ |a| a.map(&:to_i) }  # foo => [[1,2,3,],[4,5,6],[7,8,9]], but grid is still the original

如果要就地更新网格:

grid.each { |a| a.map!(&:to_i) }
于 2013-03-21T20:38:54.433 回答