13

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?

11
  • 1
    so what is the question? Commented Apr 25, 2011 at 21:14
  • 1
    @fl00r Whether or not there is a built in way to do it. Commented Apr 25, 2011 at 21:15
  • 3
    @Rein Henrichs, okey there is compact! method :) Commented Apr 25, 2011 at 21:20
  • 1
    What's wrong with just adding a new method to the Array class? Commented Apr 25, 2011 at 21:22
  • 1
    Nothing wrong if it is well documented before usage and if it is clear for all teamworkers :) Commented Apr 25, 2011 at 21:23

9 Answers 9

41

There is a short form

array.delete_if(&:empty?)
Sign up to request clarification or add additional context in comments.

2 Comments

If you are using Rails, array.delete_if(&:blank?) is a better option to avoid a NoMethodError if one of the values is nil.
array.compact.delete_if(&:empty?) will handle the nil and the blank in pure Ruby.
8

You can use this method:

    1.9.3p194 :001 > ["", "A", "B", "C", ""].reject(&:empty?)
    => `["A", "B", "C"]`

Please note that you can use the compact method if you only got to clear an array from nils.

Comments

5

Well, there is Array.delete. It returns what's deleted (or nil if nothing is deleted) however, which feels clumsy. But it does deliver and does not fail on non-string elements:

ar = ['a', '', 2, 3, '']
p ar.delete('')  #=> ""
p ar             #=> ["a", 2, 3]

1 Comment

I like this one. Some weeks ago on SO was discussion about method that deletes an element and returns itself. like array.delete(element); array
1

You can do this

ar = ['a', '', 2, 3, '']
ar = ar.select{|a| a != ""}

I hope this will work for you

Comments

0

You can use .select! but you're still going to run into the same problem.

Instead of modifying array, you could create a utility class instead.

Comments

0

You can try below solution. I hope it ll help you.

array = ["","",nil,nil,2,3] array.delete_if(&:blank?) => [2,3]

Comments

0

If you also want to remove nil:

arr = ['',"",nil,323]
arr.map!{|x|x==''?nil:x}.compact!

=> [323]

Map, ternary operator, compact

1 Comment

Unlike the other methods, this line evaluates as the returned array rather than what has been removed
0

For simple work around:

my_array = ['a', '', 2, 3, '']
compact_array = my_array.select(&:present?)
# => ["a", 2, 3]

Here:

We select only item of array that ruby think it's present while nil and "" is not present? in ruby

3 Comments

.present? is a rails method.
Another solution: ['a', '', 2, 3, ''] - ['']
0

As of Rails 5.2, Enumerable now supports .compact_blank, which does precisely what you're asking for:

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

It works on hashes as well, removing pairs that have a blank value.

Docs: https://apidock.com/rails/Enumerable/compact_blank

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.