Ben
Ben

Reputation: 25807

How can I avoid iterating over a nil array in Ruby?

I want avoid iterating over a nil array.

My bad solution:

if nil!=myArr
    myArr.each { |item|
      p item;
    }
 end

Upvotes: 13

Views: 18475

Answers (6)

Michael Berkowski
Michael Berkowski

Reputation: 270599

For a simple one-liner, you might also use unless myArr.nil?

myArr.each { |item| p item } unless myArr.nil?

Updating for Ruby >= 2.3.0:

If you are comfortable with a nil return rather than avoiding execution entirely, you can use the Safe navigation operator &.. Note though that this differs slightly. Whereas the unless version will skip execution entirely, the &. will execute but return nil.

myArr&.each { |item| p item }
# returns nil

Upvotes: 29

Cereal
Cereal

Reputation: 3829

You can wrap the array value in Array().

Array(myArr).each { |item| p item }

As per the documentation, does this:

An array can also be created by using the Array() method, provided by Kernel, which tries to call to_ary, then to_a on its argument.

Basically, this will convert a nil value to []. Which would neither iterate, or throw an error. Fair warning, it will do the same to any value. Array("string") will create ["string"].

Upvotes: 13

GetSet
GetSet

Reputation: 532

Simply checking for nil isn't always sufficient. Sometimes a variable you expect to be an array can be initialized as a non-array object when there is only one. It's not common, but proprietary services I've seen might give you a result of nil, "Name1", or ["Name1", "Name2", ...]. To reliably handle this range of input, I prefer to access my arrays like this:

Array.wrap(myArr).each { |item|
  p item
}

Array.wrap will convert nil to [], Object to [Object], and leave existing arrays alone. Also handy for not silently butchering your hashes if one gets passed in instead of an array. (Calling Array(myArr) will convert myArr into an array, which destroys hashes rather than wrapping them in arrays.

Upvotes: 3

Sergey
Sergey

Reputation: 11908

myArr ||= []

and then iterate. This will assign empty array to myArr only if it's nil.

Upvotes: 1

markijbema
markijbema

Reputation: 4055

Alternatively, using andand

myArr.andand.each { | item| p item }

Upvotes: 2

xdazz
xdazz

Reputation: 160833

In ruby, only nil and false are considered as false.

if myArr
    myArr.each { |item|
      p item
    }
end

Upvotes: 15

Related Questions