Reputation: 21
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
Reputation: 22225
Add a && nil
after your expression:
myarray.each {....} && nil
Upvotes: 0
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
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