Skip to content

Instantly share code, notes, and snippets.

@IanWhitney
Created February 14, 2013 19:42
Show Gist options
  • Select an option

  • Save IanWhitney/4955714 to your computer and use it in GitHub Desktop.

Select an option

Save IanWhitney/4955714 to your computer and use it in GitHub Desktop.
Using inject to take two arrays and use them to populate a hash
@required_keys = [:email_address,:subject]
@optional_keys = [:email_from]
@params = {:email_address=>"test@test.com", :subject=>"email subject", :email_from=>"hi@test.com", :extra=>"not used"}
all_params = (@required_keys + @optional_keys).inject({}) do |result, element|
result[element] = @params[element]
result
end
=> {:email_address=>"test@test.com", :subject=>"email subject", :email_from=>"hi@test.com"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment