intridea/hashie — pull request #133 by mhuggins (2014-02-12 18:15:13+00:00)
Title: Fix broken Hashie::Hash#to_hash(:symbolize_keys => true)

Per [my comment on the original pull request for this feature](https://github.com/intridea/hashie/pull/85#issuecomment-34896211):

> I'm running into an issue with this code. I'm trying to take a basic hash with string keys and convert it to a basic hash with symbol keys. Take the following example.
>
> ```
> hash = {"id": 1, "name": "Bob"}
> hash = Hashie::Hash[hash].to_hash(symbolize_keys: true)
> ```
>
> This throws an exception in Hashie's to_hash method using the code provided here. The reason is that on line 15 the value of `k` is being changed from `"id"` or `"name"` to `:id` or `:name` respectively. Then on line 17, the call to `self[k]` returns `nil` since `hash[:id]` is not defined (only `hash["id"]` is defined), which then errors when trying to evaluate `nil.each`.
>
> I think a proper fix would be to assign the key to a NEW variable instead. e.g.:
>
> ```
> assignment_key = options[:symbolize_keys] ? k.to_sym : k.to_s
> out[assignment_key] ||= []
> self[k].each do |array_object|
> out[assignment_key] << (Hash === array_object ? array_object.to_hash : array_object)
> end
> ```