Skip to content

Instantly share code, notes, and snippets.

@jcamenisch
Last active August 29, 2015 14:02
Show Gist options
  • Save jcamenisch/f7a3daee296e619086c9 to your computer and use it in GitHub Desktop.
Save jcamenisch/f7a3daee296e619086c9 to your computer and use it in GitHub Desktop.
List arguments to a Ruby method
def get_inputs(a, b, c)
Hash[local_variables.map { |name| [name, binding.local_variable_get(name)] }]
end
get_inputs('one', 'two', 'three')
#=> {:a=>"one", :b=>"two", :c=>"three"}
# Or package that in a reuseable method like so:
def get_local_variables(a_binding)
Hash[a_binding.eval('local_variables').map { |name| [name, a_binding.local_variable_get(name)] }]
end
# Then pass a binding object into the method, so it can inspect the calling context:
def get_inputs(a, b, c)
get_local_variables(binding)
end
get_inputs('one', 'two', 'three')
#=> {:a=>"one", :b=>"two", :c=>"three"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment