I have a two-dimensional array looking like this:
我有一个看起来像这样的二维数组:
[true,false,false]
[false,true,false]
[false,false,true]
I wish I could substitute all the true(bool) values with 'true'(string) and all the false with 'false'
我希望我能用'true'(字符串)替换所有真(bool)值,并用'false'替换所有false
2 个解决方案
#1
3
Assuming you have an array of arrays:
假设你有一个数组数组:
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"]]
#2
6
Yes, do as below using Array#map
:
是的,使用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) }
#1
3
Assuming you have an array of arrays:
假设你有一个数组数组:
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"]]
#2
6
Yes, do as below using Array#map
:
是的,使用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) }