Skip to content

Instantly share code, notes, and snippets.

@hanachin
Last active May 11, 2020 15:34
Show Gist options
  • Save hanachin/52625cdcdcca6608bfb2b46af6672b2b to your computer and use it in GitHub Desktop.
Save hanachin/52625cdcdcca6608bfb2b46af6672b2b to your computer and use it in GitHub Desktop.
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