Skip to content

Instantly share code, notes, and snippets.

@matthuhiggins
Created November 26, 2010 23:23
Show Gist options
  • Save matthuhiggins/717340 to your computer and use it in GitHub Desktop.
Save matthuhiggins/717340 to your computer and use it in GitHub Desktop.
ruby 1.8.7 Enumerator
def do_stuff(enumerator)
# stuff
end
do_stuff([1,3,5].reverse_each)
do_stuff([1,3,5].each)
"rails".each_char.sort
%w(fun with enumerator).each_with_index.map { |value, i| "#{i+1}. #{value}" }
%w(fun with enumerator).enum_for(:each_with_index).map { |value, i| "#{i+1}. #{value}" }
class Array
[:find, :find_all, :partition, :reject, :select, :sort_by].each do |method|
class_eval <<-EOV
def #{method}_with_enumerator(*args)
if block_given?
#{method}_without_enumerator(*args)
else
Enumerable::Enumerator.new(self, :enumerator, *args)
end
end
EOV
alias_method_chain method
end
end
n.downto(1).inject(:*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment