-
-
Save apeiros/6e2984f24e31b15aa3cc 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 Binding | |
def lvar_hash(*slice) | |
(slice.empty? ? self.eval("local_variables") : slice).map { |name| [name, self.local_variable_get(name)] }.to_h | |
end | |
end | |
class Object | |
def send_with(a_binding, method_name, *args, **kwargs, &block) | |
kwarg_names = method(:foo).parameters.select { |t,n| [:keyreq, :key].include?(t) }.map(&:last) | |
send(method_name, *args, **a_binding.lvar_hash(*kwarg_names).merge(kwargs), &block) | |
end | |
end |
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
def foo(a: 1, b: 2, c:) | |
p(a: a, b: b, c: c) | |
end | |
a = 10 | |
b = 20 | |
c = 30 | |
send_with(binding, :foo) | |
# -> {:a=>10, :b=>20, :c=>30} | |
send_with(binding, :foo, c: 300) | |
# -> {:a=>10, :b=>20, :c=>300} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment