Skip to content

Instantly share code, notes, and snippets.

@apeiros
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/6e2984f24e31b15aa3cc to your computer and use it in GitHub Desktop.
Save apeiros/6e2984f24e31b15aa3cc to your computer and use it in GitHub Desktop.
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
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