Last active
May 11, 2020 15:34
-
-
Save hanachin/52625cdcdcca6608bfb2b46af6672b2b 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
require 'binding_of_caller' | |
using Module.new { | |
refine(Array) do | |
def map(&block) | |
caller = binding.of_caller(1) | |
cleanroom = Class.new(BasicObject) do | |
def respond_to?(*); true; end | |
def respond_to_missing?(*); true; end | |
define_method(:method_missing) do |name,*| | |
caller.method(name) | |
end | |
end | |
super(&cleanroom.new.instance_eval(&block)) | |
end | |
end | |
} | |
def plus_one(x) | |
x + 1 | |
end | |
def double(x) | |
x * 2 | |
end | |
pp [1, 2, 3, 4].map { plus_one >> double } | |
#=> [4, 6, 8, 10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment