Skip to content

Instantly share code, notes, and snippets.

@threeifbywhiskey
Last active August 29, 2015 13:56
Show Gist options
  • Save threeifbywhiskey/8893626 to your computer and use it in GitHub Desktop.
Save threeifbywhiskey/8893626 to your computer and use it in GitHub Desktop.
Enumerabull pollutes Kernelspace with inverted versions of Enumerable's instance methods. It's like unleashing a bull in your Ruby shop to get "functional programming".
module Kernel
Enumerable.instance_methods.each do |meth|
define_method(meth) do |*args|
obj = args.pop
case pred = args.first
when Proc, Symbol
obj.send meth, *args.drop(1), &pred
else
obj.send meth, *args
end
end
end
alias each each_entry
end
p reduce :+, 10, 1..10
p select :even?, 1..10
each->n { p n }, 1..10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment