Skip to content

Instantly share code, notes, and snippets.

@epitron
Created April 14, 2010 08:47
Show Gist options
  • Save epitron/365603 to your computer and use it in GitHub Desktop.
Save epitron/365603 to your computer and use it in GitHub Desktop.
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