Today I Learned

hashrocket A Hashrocket project

HashWithIndifferentAccess for keyword arguments

HashWithIndifferentAccess is a cool hash-like object that allows you to access values with either a symbol OR a string. But you can't pass it to a method that expects keyword arguments.

def foo(a:, b:); return a, b; end
h ={a: 1, b: 2})
=> ArgumentError: wrong number of arguments (1 for 0)

Fear not!

=> 1, 2

Calling symbolize_keys on a HashWithIndifferentAccess object will create a hash that can be used for keyword arguments.

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.