Dup, Don't Modify Input Hash Parameters in Ruby

1 · Daniel Doubrovkine (dB.) @dblockdotorg · Nov. 11, 2020, 3:40 p.m.
This comes up all the time. Here’s why you always want to dup input options in Ruby that come in as a Hash. def historical_prices(symbol, options = {}) path = "stock/#{symbol}/chart" path += "/#{options[:range]}" if options.key?(:range) options.delete(:range) get(path, options) end Seems innocent enough. But consider the following. options = { range: 'year' } msft_prices = historical_prices('MSFT', options) goog_prices = historical_prices('GOOG', options) You can see the problem: the sec...