Created
December 14, 2018 15:14
-
-
Save pocke/fa37768f6db6ae43d59e94e054636f1b 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 Module | |
| def to_enum(method_name) | |
| original_method_name = :"__{method_name}_orig__" | |
| alias_method original_method_name, method_name | |
| define_method(method_name) do |*args, &block| | |
| return enum_for(method_name, *args) unless block | |
| __send__ original_method_name, *args, &block | |
| end | |
| end | |
| end | |
| class A | |
| def initialize(array) | |
| @array = array | |
| end | |
| to_enum def each_with_plus(n) | |
| @array.each do |item| | |
| yield item + n | |
| end | |
| end | |
| end | |
| A.new([1,2,3]).each_with_plus(10) do |n| | |
| p n | |
| end | |
| p(A.new([1,2,3]).each_with_plus(10).map do |n| | |
| n | |
| end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment