在 Ruby 中删除数组中的 nil 和空白字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26114332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:35:13  来源:igfitidea点击:

Remove nil and blank string in an array in Ruby

rubyarrays

提问by Cary Swoveland

I am new to Ruby and stuck with this issue. Let's say I have an array like this:

我是 Ruby 的新手并且一直在解决这个问题。假设我有一个这样的数组:

arr = [1, 2, 's', nil, '', 'd']

and I want to remove nil and blank string from it, i.e. final array should be:

我想从中删除 nil 和空白字符串,即最终数组应该是:

arr = [1, 2, 's', 'd']

I tried compactbut it gives this:

我试过了,compact但它给出了这个:

arr.compact!
arr #=> [1, 2, 's', '', 'd'] doesn't remove empty string.

I was wondering if there's a smart way of doing this in Ruby.

我想知道在 Ruby 中是否有一种聪明的方法来做到这一点。

回答by Cary Swoveland

You could do this:

你可以这样做:

arr.reject { |e| e.to_s.empty? } #=> [1, 2, "s", "d"]

Note nil.to_s => ''.

注意nil.to_s => ''

回答by awendt

Since you want to remove both niland empty strings, it's not a duplicate of How do I remove blank elements from an array?

由于您要同时删除nil和空字符串,因此它不是如何从数组中删除空白元素的副本

You want to use .reject:

你想使用.reject

arr = [1, 2, 's', nil, '', 'd']
arr.reject { |item| item.nil? || item == '' }

NOTE:rejectwith and without bang behaves the same way as compactwith and without bang: reject!and compact!modify the array itself while rejectand compactreturn a copy of the array and leave the original intact.

注意:reject有和没有爆炸的行为方式相同,compact有和没有爆炸:reject!compact!修改数组本身,而rejectcompact返回数组的副本,并保留原始不变。

If you're using Rails, you can also use blank?. It was specifically designed to work on nil, so the method call becomes:

如果你使用 Rails,你也可以使用blank?. 它专门设计用于处理nil,因此方法调用变为:

arr.reject { |item| item.blank? }

回答by JayJay

I tend to do:

我倾向于这样做:

arr = [1, 2, 's', nil, '', 'd']
arr.reject(&:blank?)

returns:

返回:

=> [1, 2, "s", "d"]

回答by Manindra Gautam

arr.reject(&:blank?)

arr.reject(&:blank?)

Just use this, no need to anything else.

就用这个,不需要别的。

回答by potashin

You can also use -to remove all niland ''elements:

您还可以使用-删除所有nil''元素:

arr -= [nil, '']
#=> [1, 2, "s", "d"]

Demonstration

示范

Or compactand rejectwith shortcut (in case you are not using Rails where you can just use arr.reject(&:blank?)):

或者compactreject与快捷方式(如果你不使用Rails,你可以只使用arr.reject(&:blank?)

arr = arr.compact.reject(&''.method(:==))
#=> [1, 2, "s", "d"]

Demonstration

示范

回答by RockStar

You can use compact with reject

您可以将 compact 与拒绝一起使用

arr = [1, 2, 's', nil, '', 'd']
arr = [1, 2, 's', 'd']

arr = arr.compact.reject { |h| h == "" }

or

或者

arr = arr.compact.delete_if { |h| h == "" }

回答by Jigar Panchal

The simplest and fast way of doing this is :

最简单快速的方法是:

arr = [1, 2, 's', nil, '', 'd'] - [nil,'']
==> arr = [1, 2, 's', 'd']

回答by padmapriya

You can use compact and delete_if method to remove nil and blank string in an array in Ruby

您可以使用 compact 和 delete_if 方法在 Ruby 中删除数组中的 nil 和空白字符串

arr = [1, 2, 's', nil, '', 'd']
arr.compact!.delete_if{|arrVal| arrVal.class == String and arrVal.empty?}
=> [1, 2, "s", "d"]

回答by Varun Gupta

Hope this will work for your case :

希望这对您的情况有用:

arr = [1, 2, 's', nil, '', 'd']
arr.select{|x| x.to_s!="" }

回答by Dom

I would probably add .stripto eliminate potential whitespace headaches (assuming its not a rails app).

我可能会添加.strip以消除潜在的空白问题(假设它不是 Rails 应用程序)。

array = [1, 2, "s", nil, "     ", "d", "\n"]
array.reject!{|a| a.nil? || (a.to_s.strip.empty?) }

#=> [1, 2, "s", "d"]