Valentine M.
Valentine M.

Reputation: 21

Return value of iteration over array

I'm trying to build a music library.

My main problem is when I iterate over the array with the #each method the return value is huge knowing that my array is all the albums and songs about one artist.

Would you know a way to iterate over arrays with a return value of nil or at least way shorter than the entire artist array I created?

Upvotes: 0

Views: 134

Answers (3)

user1934428
user1934428

Reputation: 22225

Add a && nil after your expression:

myarray.each {....} && nil

Upvotes: 0

omikes
omikes

Reputation: 8513

Compact method will eliminate nil values, if that is what you mean.

['foo', nil, 'bar'].compact.each do |part|
    puts part
end

=> foo
   bar

Upvotes: 0

Ursus
Ursus

Reputation: 30056

Just return nil after the iteration if you don't want the collection as a return value.

def your_method
  your_collection.each do |item|
    # do something
  end
  nil
end

Upvotes: 4

Related Questions