Created
November 26, 2010 23:23
-
-
Save matthuhiggins/717340 to your computer and use it in GitHub Desktop.
ruby 1.8.7 Enumerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def do_stuff(enumerator) | |
# stuff | |
end | |
do_stuff([1,3,5].reverse_each) | |
do_stuff([1,3,5].each) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"rails".each_char.sort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w(fun with enumerator).each_with_index.map { |value, i| "#{i+1}. #{value}" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w(fun with enumerator).enum_for(:each_with_index).map { |value, i| "#{i+1}. #{value}" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n.downto(1).inject(:*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment