method
to_hash
v4.2.7 -
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 249
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
protected
def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end
def convert_value(value, options = {})
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)
if default_proc
target.default_proc = default_proc.dup
else
target.default = default
end
end
end Related methods
- Instance methods
- []=
- convert_key
- convert_value
- deep_stringify_keys
- deep_stringify_keys!
- deep_symbolize_keys
- default
- delete
- dup
- extractable_options?
- fetch
- key?
- merge
- nested_under_indifferent_access
- regular_update
- regular_writer
- reject
- replace
- reverse_merge
- reverse_merge!
- select
- set_defaults
- stringify_keys
- stringify_keys!
- symbolize_keys
- to_hash
- to_options!
- update
- values_at
- with_indifferent_access
- Class methods
- []
- new
- new_from_hash_copying_default