Skip to content

Instantly share code, notes, and snippets.

@tkwidmer
Created May 28, 2014 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkwidmer/44b49af34bce75165a87 to your computer and use it in GitHub Desktop.
Save tkwidmer/44b49af34bce75165a87 to your computer and use it in GitHub Desktop.
is there a cleaner way to do an each but only if the array exists
if array
array.each do | elm |
elm.doing_stuff
end
end
@jaredonline
Copy link

If you're in Rails, this should work:

array.try(:each) do |elm|
  elm.doing_stuff
end

@miah
Copy link

miah commented May 28, 2014

array.each { |elm| elm.doing.stuff } if array

@seanlinsley
Copy link

Another approach:

array.each do |elm|
  elm.doing_stuff
end if array

@tkwidmer
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment