문제

I have a two-dimensional array of a bunch of strings, including some empty strings. I want to replace the empty strings with nil. How do I do this in Ruby?

Sample array:

[['cat','','dog',''],['','','fish','','horse']]

Desired output:

[['cat',nil,'dog',nil],[nil,nil,'fish',nil,'horse']]
도움이 되었습니까?

해결책

[['cat','','dog',''],['','','fish','','horse']].map do |arr|
  arr.map { |s| s unless s.empty? }
end
# => [["cat", nil, "dog", nil], [nil, nil, "fish", nil, "horse"]]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top