Skip to content

Instantly share code, notes, and snippets.

@Bajena
Last active July 27, 2016 09:08
Show Gist options
  • Save Bajena/cb5c953e706e661089b37cbb85955087 to your computer and use it in GitHub Desktop.
Save Bajena/cb5c953e706e661089b37cbb85955087 to your computer and use it in GitHub Desktop.
get a hash of keyword arguments method in ruby
module ArgsToHash
# allows returning a hash of arguments
# Example:
# def method(a:, b:)
# puts eval(ArgsToHash::ARGS_TO_HASH)
# end
#
# method(a: 1, b: 2)
# => {:a=>1, :b=>2}
ARGS_TO_HASH = "method(__method__).parameters.map { |arg| arg[1].to_s }."\
"map { |arg| { arg.to_sym => eval(arg) } }."\
"reduce Hash.new, :merge".freeze
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment