简体   繁体   English

在Ruby中用Nil替换数组中的空单元格?

[英]Replace Empty Cells in Array With Nil in Ruby?

I have a two-dimensional array of a bunch of strings, including some empty strings. 我有一堆字符串的二维数组,包括一些空字符串。 I want to replace the empty strings with nil. 我想用nil替换空字符串。 How do I do this in Ruby? 我如何在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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM