我有一个看起来像这样的二维数组:
[true,false,false]
[false,true,false]
[false,false,true]
我希望我可以用“真”(字符串)替换所有真(布尔)值,用“假”替换所有假
我有一个看起来像这样的二维数组:
[true,false,false]
[false,true,false]
[false,false,true]
我希望我可以用“真”(字符串)替换所有真(布尔)值,用“假”替换所有假
是的,使用以下方法执行以下操作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) }
假设您有一个数组数组:
a = [[true,false,false], [false,true,false], [false,false,true]]
a.each { |x| x.map!(&:to_s) }
a # => [["true", "false", "false"], ["false", "true", "false"], ["false", "false", "true"]]