0

次のような 2 次元配列があります。

[true,false,false]
[false,true,false]
[false,false,true]

すべてのtrue(bool)値を'true'(string)に置き換え、すべてのfalse値を'false'に置き換えたいと思います

4

2 に答える 2

5

はい、以下を使用して実行します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) } 
于 2014-01-19T14:12:22.843 に答える
3

配列の配列があると仮定します:

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"]]
于 2014-01-19T14:13:59.437 に答える