Skip to content

Instantly share code, notes, and snippets.

@cyakimov
Created June 10, 2011 16:09
Show Gist options
  • Save cyakimov/1019169 to your computer and use it in GitHub Desktop.
Save cyakimov/1019169 to your computer and use it in GitHub Desktop.
Filter empty strings and nils from array in Ruby
#["",nil,"something","another",123,""] => ["something","another",123]
array_to_filter.reject &:empty?
@momolog
Copy link

momolog commented Mar 3, 2014

Will not filter nils: NoMethodError: undefined method `empty?' for nil:NilClass

@aimee-ault
Copy link

Switch empty? to blank? if you want it to work with nil.

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