Last active
October 19, 2015 01:32
-
-
Save FayeAlephNil/e0a23291c5560d4f871e to your computer and use it in GitHub Desktop.
Ideas about binding.of_caller
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' | |
module A | |
refine String do | |
def plusmeow | |
self + 'meow' | |
end | |
def test_method | |
make(self, 'p a; p b; p c; p this.plusmeow', :b, :c) | |
end | |
end | |
end | |
def make(this, s, *params1) | |
->(*params2) { | |
p1size = params1.size | |
p2size = params2.size | |
raise ArgumentError.new("(#{p2size} for #{p1size})") if p1size != p2size | |
_binding = binding.of_caller(1).clone | |
params1.each_with_index { |param, i| | |
_binding.local_variable_set param, params2[i] | |
} | |
_binding.local_variable_set :this, this | |
_binding.eval s | |
} | |
end | |
using A | |
a = 1 | |
"meow".test_method[2, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment