method
to_hash
v6.0.0 -
Show latest stable
- Class:
ActiveSupport::HashWithIndifferentAccess
to_hash()public
Convert to a regular hash with string keys.
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 355
def to_hash
_new_hash = Hash.new
set_defaults(_new_hash)
each do |key, value|
_new_hash[key] = convert_value(value, for: :to_hash)
end
_new_hash
end
private
def convert_key(key) # :doc:
key.kind_of?(Symbol) ? key.to_s : key
end
def convert_value(value, options = {}) # :doc:
if value.is_a? Hash
if options[:for] == :to_hash
value.to_hash
else
value.nested_under_indifferent_access
end
elsif value.is_a?(Array)
if options[:for] != :assignment || value.frozen?
value = value.dup
end
value.map! { |e| convert_value(e, options) }
else
value
end
end
def set_defaults(target) # :doc:
if default_proc
target.default_proc = default_proc.dup
else
target.default = default
end
end
end Related methods
- Instance methods
- []
- []=
- assoc
- compact
- convert_key
- convert_value
- deep_stringify_keys
- deep_stringify_keys!
- deep_symbolize_keys
- default
- delete
- dig
- dup
- except
- extractable_options?
- fetch
- fetch_values
- key?
- merge
- nested_under_indifferent_access
- regular_update
- regular_writer
- reject
- replace
- reverse_merge
- reverse_merge!
- select
- set_defaults
- slice
- slice!
- stringify_keys
- stringify_keys!
- symbolize_keys
- to_hash
- to_options!
- transform_keys
- transform_keys!
- transform_values
- update
- values_at
- with_indifferent_access
- Class methods
- []
- new