从一个数组的数组这是空的空白删除所有项目 [英] Removing all items from an array of arrays which are empty of blank

查看:165
本文介绍了从一个数组的数组这是空的空白删除所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组的数组,我想删除所有具有这是剥离后的空间为零或空元素的项目。看看这个片断:

I have an array of arrays and I would like to remove all the items that have elements which are nil or empty after stripping spaces. Look at this snippet:

x = Array.new
x << ["A","B", " ", "D"]
x << ["","  ", nil, ""]
x << ["E","Q", "F", "M"]

我想删除第二个记录,因为它不包含真实的数据。

I would like to remove the second record because, it contains no real data.

什么是做到这一点的最好方法是什么?我应该简单地迭代这个数组和写的if-else条件测试?

What would be the best way to do this? Should I simply iterate over the array and and write if-else conditions to test?

推荐答案

如果使用纯Ruby中,你可以做

If using plain Ruby, you can do

x.reject{ |arr| arr.all? {|elem| elem.nil? || elem.strip.empty? }}

如果使用Rails,由于辅助方法,你可以做

If using rails, thanks to the helper methods you can do

x.reject{ |arr| arr.all?(&:blank?) }

关键方法是 拒绝 空白?

这篇关于从一个数组的数组这是空的空白删除所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆