Last active
December 1, 2017 01:53
-
-
Save nobu/91d8dcac60b95de3f6df to your computer and use it in GitHub Desktop.
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 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