Вопрос

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'

Это было полезно?

Решение 2

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"]]

Другие советы

Yes, do as below using 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) } 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top