如何删除数组中的空字符串?

时间:2022-01-12 07:21:54

I want to get delete_if to delete empty strings form an array. With the solution below, the array still contains many empty strings.

我想要获取delete_if来删除数组中的空字符串。有了下面的解决方案,数组仍然包含许多空字符串。

products = my_text.split(/\t+/)
products.delete_if {|element| element == " " || "" || element.nil?}

Is there anything missing?

有遗漏什么吗?

2 个解决方案

#1


8  

The problem with your code is explained by Ed S.
Otherwise, you can do

您的代码的问题由Ed S.来解释,否则,您可以这样做

products.reject! { |s| s.nil? || s.strip.empty? }

Why do you need to test nil? first? Let's check few lines.

为什么需要测试nil?第一位?让我们检查几行。

nil.strip
# NoMethodError: undefined method `strip' for nil:NilClass
"   ".strip
# => ""

Now, with a different order, what the code does if the object is a string, and then if it is nil.

现在,有了不同的顺序,如果对象是字符串,代码会做什么,如果它是nil。

"  ".strip || "  ".nil?
# => ""
nil.strip || nil.nil?
# NoMethodError: undefined method `strip' for nil:NilClass
# Oh you don't want that to happen, do you?

This means you don't want to call strip.empty? when your object is nil.
And as you know, when you have a || b, if a is truthy (i.e. not nil nor false), b will never be called.
You test first if the string is nil ; if it is, you don't need to check the right part (so you won't get a undefined method error) and the object will be removed from your products list.

这意味着你不想调用strip.empty?当你的对象是nil。如你所知,当你有|| b时,如果a是真实的(即不是nil也不是假的),b将永远不会被调用。首先测试字符串是否为nil;如果是,则不需要检查正确的部分(这样就不会出现未定义的方法错误),对象将从产品列表中删除。

#2


2  

Well this is wrong:

这是错误的:

element == " " || "" || element.nil?

Should be

应该是

products = products.delete_if {|element| element == " " || element == "" || element.nil?

Note that you had a || "" || in there. You weren't comparing element to "", you were testing the "truthiness" of "" (which evaluates to true btw, screwing up your empty string check).

注意这里有|| " ||。您没有将元素与“”进行比较,您正在测试“”(它的计算结果为true btw,将您的空字符串检查弄乱)的“truthiness”。

This of course assumes your definition of an "empty string" is either nil, " ", or "". What about

这当然假定您对“空字符串”的定义是“nil”、“”或“”。是什么

"  " 

or even

甚至

"         "

?

吗?

#1


8  

The problem with your code is explained by Ed S.
Otherwise, you can do

您的代码的问题由Ed S.来解释,否则,您可以这样做

products.reject! { |s| s.nil? || s.strip.empty? }

Why do you need to test nil? first? Let's check few lines.

为什么需要测试nil?第一位?让我们检查几行。

nil.strip
# NoMethodError: undefined method `strip' for nil:NilClass
"   ".strip
# => ""

Now, with a different order, what the code does if the object is a string, and then if it is nil.

现在,有了不同的顺序,如果对象是字符串,代码会做什么,如果它是nil。

"  ".strip || "  ".nil?
# => ""
nil.strip || nil.nil?
# NoMethodError: undefined method `strip' for nil:NilClass
# Oh you don't want that to happen, do you?

This means you don't want to call strip.empty? when your object is nil.
And as you know, when you have a || b, if a is truthy (i.e. not nil nor false), b will never be called.
You test first if the string is nil ; if it is, you don't need to check the right part (so you won't get a undefined method error) and the object will be removed from your products list.

这意味着你不想调用strip.empty?当你的对象是nil。如你所知,当你有|| b时,如果a是真实的(即不是nil也不是假的),b将永远不会被调用。首先测试字符串是否为nil;如果是,则不需要检查正确的部分(这样就不会出现未定义的方法错误),对象将从产品列表中删除。

#2


2  

Well this is wrong:

这是错误的:

element == " " || "" || element.nil?

Should be

应该是

products = products.delete_if {|element| element == " " || element == "" || element.nil?

Note that you had a || "" || in there. You weren't comparing element to "", you were testing the "truthiness" of "" (which evaluates to true btw, screwing up your empty string check).

注意这里有|| " ||。您没有将元素与“”进行比较,您正在测试“”(它的计算结果为true btw,将您的空字符串检查弄乱)的“truthiness”。

This of course assumes your definition of an "empty string" is either nil, " ", or "". What about

这当然假定您对“空字符串”的定义是“nil”、“”或“”。是什么

"  " 

or even

甚至

"         "

?

吗?