从数组中清除空字符串 [英] Clearing Empty Strings from an Array
本文介绍了从数组中清除空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一堆由字符串组成的数组,而我多次写过.delete_if { |str| str.empty? }
I'm dealing with a bunch of arrays made of strings and many a times I've written .delete_if { |str| str.empty? }
现在,我知道我可以自己将此方法添加到数组类中,但是我希望有一种内置的方法可以执行此操作,而无需在基类中添加非标准方法.除了向基类添加方法之外,它还很有趣,但出于可维护性的原因,我不想这么做.
Now, I know I can add this method to the array class myself but I'm hoping there's a built in way to do this without having non-standard methods added to base classes. As much fun as adding methods to base classes is, it's not something I wanna do for maintainability reasons.
有内置的方法可以处理吗?
Is there a built in method to handle this?
推荐答案
有一个简短格式
array.delete_if(&:empty?)
这篇关于从数组中清除空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文