It's better for at least a couple of reasons, eloquently stated in this blog post by Michal Orman
...we can set default values or handle - providing a block - gracefully missing keys. Also using
fetchwith unknown key will raise
KeyErrorthat will tell us which exactly key is missing. That is in fact the behavior we are expecting from the app. Without required settings is just not working and complaining about missing setting and not about some random nil references.
Got that? Instead of this:
AWS.config( access_key_id: ENV['S3_ACCESS_KEY'], secret_access_key: ENV['S3_SECRET_KEY'], region: ENV['S3_REGION'] )
AWS.config( access_key_id: ENV.fetch('S3_ACCESS_KEY'), secret_access_key: ENV.fetch('S3_SECRET_KEY'), region: ENV.fetch('S3_REGION') )
Thoughtbot has the best writeup.
I suspect I will not be using non-keyword arguments in my Ruby code anymore.
Today I woke up to an exception
RuntimeError: can't modify frozen String
I looked at the code that had raised the exception. It was doing a
gsub! on a value out of params. It took me a bit of digging to figure out why it failed. Turns out there's an edge case that causes one of those values to get pulled out of
ENV instead of the normal flow where it originates from the user.
Strings coming out of ENV are frozen. D'oh!