Last active
July 27, 2016 09:08
-
-
Save Bajena/cb5c953e706e661089b37cbb85955087 to your computer and use it in GitHub Desktop.
get a hash of keyword arguments method in ruby
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
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