Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/active_support/core_ext/hash/keys.rb,
lib/active_support/core_ext/hash/indifferent_access.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
-
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
-
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys ⇒ Object
(also: #to_options)
Return a new hash with all keys converted to symbols, as long as they respond to
to_sym. -
#symbolize_keys! ⇒ Object
(also: #to_options!)
Destructively convert all keys to symbols, as long as they respond to
to_sym. -
#with_indifferent_access ⇒ Object
(also: #nested_under_indifferent_access)
Returns an
ActiveSupport::HashWithIndifferentAccessout of its receiver:.
Instance Method Details
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch. Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols as keys, this will fail.
Examples
{ :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
{ :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key: name"
{ :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
48 49 50 51 52 53 |
# File 'lib/active_support/core_ext/hash/keys.rb', line 48 def assert_valid_keys(*valid_keys) valid_keys.flatten! each_key do |k| raise(ArgumentError, "Unknown key: #{k}") unless valid_keys.include?(k) end end |
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
{ :name => 'Rob', :years => '28' }.stringify_keys
#=> { "name" => "Rob", "years" => "28" }
6 7 8 |
# File 'lib/active_support/core_ext/hash/keys.rb', line 6 def stringify_keys dup.stringify_keys! end |
#stringify_keys! ⇒ Object
Destructively convert all keys to strings. Same as stringify_keys, but modifies self.
12 13 14 15 16 17 |
# File 'lib/active_support/core_ext/hash/keys.rb', line 12 def stringify_keys! keys.each do |key| self[key.to_s] = delete(key) end self end |
#symbolize_keys ⇒ Object Also known as: to_options
Return a new hash with all keys converted to symbols, as long as they respond to to_sym.
{ 'name' => 'Rob', 'years' => '28' }.symbolize_keys
#=> { :name => "Rob", :years => "28" }
24 25 26 |
# File 'lib/active_support/core_ext/hash/keys.rb', line 24 def symbolize_keys dup.symbolize_keys! end |
#symbolize_keys! ⇒ Object Also known as: to_options!
Destructively convert all keys to symbols, as long as they respond to to_sym. Same as symbolize_keys, but modifies self.
30 31 32 33 34 35 |
# File 'lib/active_support/core_ext/hash/keys.rb', line 30 def symbolize_keys! keys.each do |key| self[(key.to_sym rescue key) || key] = delete(key) end self end |
#with_indifferent_access ⇒ Object Also known as: nested_under_indifferent_access
Returns an ActiveSupport::HashWithIndifferentAccess out of its receiver:
{:a => 1}.with_indifferent_access["a"] # => 1
8 9 10 |
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 8 def with_indifferent_access ActiveSupport::HashWithIndifferentAccess.(self) end |