diff options
author | Corprew Reed <github@corprew.org> | 2019-05-27 23:44:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-27 23:44:53 -0700 |
commit | 82ee7a0fa93e6f9ec69804734c8981217587dae5 (patch) | |
tree | a5ef62d7010aa4a0ce9972b664590b426bf3e6b0 | |
parent | f09ca65e0f298b39feecf99347deaf2acd2acc10 (diff) | |
download | rails-82ee7a0fa93e6f9ec69804734c8981217587dae5.tar.gz rails-82ee7a0fa93e6f9ec69804734c8981217587dae5.tar.bz2 rails-82ee7a0fa93e6f9ec69804734c8981217587dae5.zip |
stringify_keys and symbolize_keys have stable results.
Rails 6 uses the `Hash.transform_keys` found in Ruby 2.5 and later, and that method enumerates keys based on insertion order. Calling `symbolize_keys`, `stringify_keys`, and their bang variants will result in the same hash every time -- the value for any key where a collision occurs is the last assigned in that enumeration
In the docs for Hash -- https://ruby-doc.org/core-2.5.0/Hash.html
> Hashes enumerate their values in the order that the corresponding keys were inserted.
-rw-r--r-- | guides/source/active_support_core_extensions.md | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 1a057832d4..d46f8fb74d 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -2633,14 +2633,12 @@ The method `stringify_keys` returns a hash that has a stringified version of the # => {"" => nil, "1" => 1, "a" => :a} ``` -In case of key collision, one of the values will be chosen. The chosen value may not always be the same given the same hash: +In case of key collision, the value will be the one most recently inserted into the hash. ```ruby {"a" => 1, a: 2}.stringify_keys -# The result could either be +# The result will be # => {"a"=>2} -# or -# => {"a"=>1} ``` This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionView::Helpers::FormHelper` defines: @@ -2677,14 +2675,12 @@ The method `symbolize_keys` returns a hash that has a symbolized version of the WARNING. Note in the previous example only one key was symbolized. -In case of key collision, one of the values will be chosen. The chosen value may not always be the same given the same hash: +In case of key collision, the value will be the one most recently inserted into the hash. ```ruby {"a" => 1, a: 2}.symbolize_keys -# The result could either be +# The result will be # => {:a=>2} -# or -# => {:a=>1} ``` This method may be useful for example to easily accept both symbols and strings as options. For instance `ActionController::UrlRewriter` defines |