Skip to content

Commit

Permalink
Improve guide for Hash#transform_keys and related methods.
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
htanata committed Feb 28, 2014
1 parent f302079 commit 42417b0
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions guides/source/active_support_core_extensions.md
Expand Up @@ -2719,11 +2719,14 @@ The method `transform_keys` accepts a block and returns a hash that has applied
# => {"" => nil, "A" => :a, "1" => 1}
```

The result in case of collision is undefined:
The result in case of key collision is not guaranteed:

```ruby
{"a" => 1, a: 2}.transform_keys { |key| key.to_s.upcase }
# => {"A" => 2}, in my test, can't rely on this result though
# The result could either be
# => {"A"=>2}
# or
# => {"A"=>1}
```

This method may be useful for example to build specialized conversions. For instance `stringify_keys` and `symbolize_keys` use `transform_keys` to perform their key conversions:
Expand Down Expand Up @@ -2758,11 +2761,14 @@ The method `stringify_keys` returns a hash that has a stringified version of the
# => {"" => nil, "a" => :a, "1" => 1}
```

The result in case of collision is undefined:
The result in case of key collision is not guaranteed:

```ruby
{"a" => 1, a: 2}.stringify_keys
# => {"a" => 2}, in my test, can't rely on this result though
# The result could either 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:
Expand Down Expand Up @@ -2799,11 +2805,14 @@ 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.

The result in case of collision is undefined:
The result in case of key collision is not guaranteed:

```ruby
{"a" => 1, a: 2}.symbolize_keys
# => {:a=>2}, in my test, can't rely on this result though
# The result could either 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
Expand Down

0 comments on commit 42417b0

Please sign in to comment.