Skip to content

Instantly share code, notes, and snippets.

@nobu
Last active December 1, 2017 01:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobu/91d8dcac60b95de3f6df to your computer and use it in GitHub Desktop.
Save nobu/91d8dcac60b95de3f6df to your computer and use it in GitHub Desktop.
class Module
# call-seq:
# enumerable name
#
# Lets the method named as +name+ return an enumerator if no block is
# given.
#
# class N
# enumerable def numbers
# yield 1
# yield 2
# yield 3
# end
# end
# N.new.numbers.to_a #=> [1, 2, 3]
def enumerable(*names)
names.each do |name|
m = instance_method(name)
define_method(name) do |*args, &block|
return self.to_enum(__callee__, *args) unless block
m.bind(self).(*args, &block)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment