user4093799
user4093799

Reputation:

Remove nil and blank string in an array in Ruby

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

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

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

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

I tried compact but it gives this:

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.

Upvotes: 52

Views: 61728

Answers (13)

Marian13
Marian13

Reputation: 9228

compact_blank (Rails 6.1+)

If you are using Rails (or a standalone ActiveSupport), starting from version 6.1, there is a compact_blank method which removes blank values from arrays.

It uses Object#blank? under the hood for determining if an item is blank.

[1, 2, 's', nil, '', 'd'].compact_blank
# => [1, 2, 's', 'd']

[1, "", nil, 2, " ", [], {}, false, true].compact_blank
# => [1, 2, true]

Here is a link to the docs and a link to the relative PR.

A destructive variant is also available. See Array#compact_blank!.

Upvotes: 17

Cary Swoveland
Cary Swoveland

Reputation: 110665

You could do this:

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

Note nil.to_s => ''.

Upvotes: 60

Jigar Panchal
Jigar Panchal

Reputation: 93

The simplest and fast way of doing this is :

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

Upvotes: 2

Manindra Gautam
Manindra Gautam

Reputation: 387

arr.reject(&:blank?)

Just use this, no need to anything else.

Upvotes: 8

JayJay
JayJay

Reputation: 814

I tend to do:

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

returns:

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

Upvotes: 28

Dom
Dom

Reputation: 160

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

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

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

Upvotes: 0

Varun Gupta
Varun Gupta

Reputation: 1

Hope this will work for your case :

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

Upvotes: 0

potashin
potashin

Reputation: 44581

You can also use - to remove all nil and '' elements:

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

Demonstration

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

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

Demonstration

Upvotes: 4

RockStar
RockStar

Reputation: 1314

You can use compact with reject

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 == "" }

Upvotes: 2

Surya
Surya

Reputation: 15992

Note: I am considering the array might have string with white spaces in it.

You can do:

arr = [1, 2, 's', nil, ' ', 'd']
arr.reject{|a| a.nil? || (a.to_s.gsub(' ', '') == '') }
#=> [1, 2, "s", "d"]

or:

arr.reject{|a| a.nil? || (a.to_s.gsub(' ', '').empty?) }
#=> [1, 2, "s", "d"]

or if you want to update arr object itself then:

arr.reject!{|a| a.nil? || (a.to_s.gsub(' ', '') == '') } # notice the ! mark, it'll update the object itself.
p arr #=> [1, 2, "s", "d"]

Upvotes: 0

padmapriya
padmapriya

Reputation: 11

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

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

Upvotes: 1

Sachin Singh
Sachin Singh

Reputation: 7225

try this out:

[1, 2, "s", nil, "", "d"].compact.select{|i| !i.to_s.empty?}

Upvotes: 0

awendt
awendt

Reputation: 13633

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

You want to use .reject:

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

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

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

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

Upvotes: 27

Related Questions