Created
April 14, 2010 08:47
-
-
Save epitron/365603 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Object | |
def self.enumerable *meths | |
meths.each do |meth| | |
alias_method "#{meth}_without_enumerator", meth | |
class_eval %{ | |
def #{meth}(*args, &block) | |
return Enumerable::Enumerator.new(self, #{meth.inspect}, *args, &block) unless block_given? | |
#{meth}_without_enumerator(*args, &block) | |
end | |
} | |
end | |
end | |
end | |
class Blah | |
def stuff(&block) | |
10.times { |x| yield x } | |
end | |
enumerable :stuff | |
end | |
p Blah.new.stuff | |
p Blah.new.stuff.to_a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment