Skip to content

Instantly share code, notes, and snippets.

@bosmacs
Created October 8, 2009 15:59
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 bosmacs/205126 to your computer and use it in GitHub Desktop.
Save bosmacs/205126 to your computer and use it in GitHub Desktop.
# add list comprehensions to arrays, used like so:
# >> (1..25).to_a.comprehend { |x| x**2 if not x % 2 == 0 }
# => [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625]
class Array
def comprehend(&block)
block ? map(&block).compact : self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment