diff options
author | Michael Herold <opensource@michaeljherold.com> | 2020-10-22 22:11:34 -0500 |
---|---|---|
committer | Michael Herold <opensource@michaeljherold.com> | 2020-10-22 22:11:34 -0500 |
commit | a7d57c921dd9a3e99aff95bcae685f831cade7f9 (patch) | |
tree | fef8c25140223fbc9fc052f5c16a8091fdf551ae /lib/hashie/extensions/indifferent_access.rb | |
parent | 09581e249729cd7a962b4df47837247c79d4e7ea (diff) | |
download | hashie-a7d57c921dd9a3e99aff95bcae685f831cade7f9.tar.gz |
Allow exporting a normal, not-indifferent Hash
Following the conventions in `activesupport` and the semantics of the
`#to_hash` method in Ruby's standard library, the `#to_hash` method for
a hash that has mixed in `IndifferentAccess` will now export the hash as
a normal, not-indifferent hash.
Diffstat (limited to 'lib/hashie/extensions/indifferent_access.rb')
-rw-r--r-- | lib/hashie/extensions/indifferent_access.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb index ebdb0f9..d6b9da3 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -143,6 +143,18 @@ module Hashie super.convert! end + def to_hash + {}.tap do |result| + each_pair { |key, value| result[key] = value } + + if default_proc + result.default_proc = default_proc + else + result.default = default + end + end + end + with_minimum_ruby('2.5.0') do def slice(*keys) string_keys = keys.map { |key| convert_key(key) } |