如何在Ruby中替换二维数组中的所有值

use*_*702 0 ruby arrays map string-substitution

我有一个看起来像这样的二维数组:

[true,false,false]
[false,true,false]
[false,false,true]
Run Code Online (Sandbox Code Playgroud)

我希望我能代替所有的真(布尔)与值"真"(串)和所有的"假"

Aru*_*hit 6

是,请按以下方式使用Array#map:

a = [[true,false,false], [false,true,false], [false,false,true]]
# you can also assign this to a new local variable instead of a,
# if you need to use your source array object in future anywhere.
a = a.map { |e| e.map(&:to_s) } 
Run Code Online (Sandbox Code Playgroud)

  • +1.这是更好/线程安全的方法. (3认同)