class Hash

Ruby’s core Hash class. See documentation for version 2.1.5, 2.0.0, or 1.9.3.

Ruby’s core Hash class. See documentation for version 2.1.5, 2.0.0, or 1.9.3.

Public Instance Methods

assert_valid_keys(*valid_keys) click to toggle source

The +#assert_valid_keys method validates that all keys in a hash match +*valid_keys+, raising an ArgumentError if the keys don’t match. Note that, as usual, symbols and strings are treated as distinct.

Examples:

{:foo => 'bar'}.assert_valid_keys(:foo)         # => true
{:foo => 'bar'}.assert_valid_keys(:foo, :baz)   # => true
{:foo => 'bar'}.assert_valid_keys(:baz)         # => ArgumentError
{:foo => 'bar'}.assert_valid_keys('foo')        # => ArgumentError
   # File lib/reactive_support/core_ext/hash/keys.rb
25 def assert_valid_keys(*valid_keys)
26   valid_keys.flatten!
27   each_key do |key|
28     unless valid_keys.include?(key) 
29       raise ArgumentError.new("Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
30     end
31   end
32 end
deep_dup() click to toggle source

When called on a hash, the #deep_dup method duplicates all the key-value pairs in the hash recursively, so that actions on the duplicate do not affect the original hash:

hash = {a: {b: 2}} # => {a: {b: 2}} dup, deep = hash.dup, hash.deep_dup # => {a: {b: 2}}, {a: {b: 2}}

deep[:b] = 14 # => {a: {b: 14}} p hash # => {a: {b: 2}}

dup[:b] = 14 # => {a: {b: 14}} p hash # => {a: {b: 14}}

   # File lib/reactive_support/core_ext/object/deep_dup.rb
56 def deep_dup
57   self.each_with_object(dup) do |(key, value), hash|
58     hash[key.deep_dup] = value.deep_dup
59   end
60 end
extractable_options?() click to toggle source

By default, only instances of Hash itself are extractable. Subclasses of Hash may implement this method and return true to declare themselves extractable. +Array#extract_options!+ then pops the hash if it comes as the last argument in a set of splat args.

   # File lib/reactive_support/core_ext/array/extract_options.rb
 9 def extractable_options?
10   instance_of? Hash 
11 end
present?() click to toggle source

When called on a hash (or any enumerable), the #present? method returns true if the hash is not empty, even if its elements have blank values:

{ foo: :bar }.present?      # => true
{ 'bar' => nil }.present?   # => true
{}.present?                 # => false
    # File lib/reactive_support/core_ext/object/blank.rb
110 def present?
111   !blank?
112 end
stringify_keys() click to toggle source

The #stringify_keys method returns a hash identical to the calling hash, but with symbol keys turned into strings. It is non-destructive; the original hash is still available after it is called.

Although this method was formerly a part of ActiveSupport, it was already deprected by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { :foo => 'bar' }
dup = orig.stringify_keys

orig      #=> { :foo => 'bar' }
dup       #=> { 'foo' => 'bar' }
   # File lib/reactive_support/core_ext/hash/keys.rb
49 def stringify_keys
50   transform_keys &:to_s
51 end
stringify_keys!() click to toggle source

The #stringify_keys! method converts symbol hash keys into strings. It is a destructive method; the original hash is changed when this method is called.

Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { :foo => 'bar' }
orig.symbolize_keys!

orig      #=> { 'foo' => 'bar' }
   # File lib/reactive_support/core_ext/hash/keys.rb
67 def stringify_keys!
68   transform_keys! &:to_s
69 end
symbolize_keys() click to toggle source

The #symbolize_keys method returns a hash identical to the calling hash but with string keys turned into symbols. It is non-destructive; the original hash is still available after it is called.

Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { 'foo' => 'bar' }
dup = orig.symbolize_keys

orig      #=> { 'foo' => 'bar' }
dup       #=> { :foo => 'bar' }
   # File lib/reactive_support/core_ext/hash/keys.rb
86 def symbolize_keys
87   transform_keys {|key| key.to_sym }
88 end
symbolize_keys!() click to toggle source

The #symbolize_keys! method converts string hash keys into symbols. It is a destructive method; the original hash is changed when this method is called.

Although this method was formerly a part of ActiveSupport, it was already deprecated by the time ReactiveSupport was introduced. For that reason, it is being included as part of ReactiveExtensions.

Examples:

orig = { 'foo' => 'bar' }
orig.symbolize_keys!

orig      #=> { :foo => 'bar' }
    # File lib/reactive_support/core_ext/hash/keys.rb
104 def symbolize_keys!
105   transform_keys! {|key| key.to_sym }
106 end
transform_keys() { |key] = self| ... } click to toggle source

The #transform_keys method returns a new hash with all keys transformed in accordance with the given block. The #transform_keys method is non-destructive; the original hash will still be available after it is called.

If no block is given, the method returns an enumerator.

Example:

hash = { :foo => 'bar' }
hash.transform_keys {|key| key.to_s.upcase }      # => { 'FOO' => 'bar' }
hash.transform_keys                               # => #<Enumerator: {:foo=>"bar"}:transform_keys>
hash                                              # => { :foo => 'bar' }
    # File lib/reactive_support/core_ext/hash/keys.rb
121 def transform_keys(&block)
122   return enum_for(:transform_keys) unless block_given?
123   dup = self.class.new 
124 
125   each_key do |key|
126     dup[yield key] = self[key]
127   end
128 
129   dup
130 end
transform_keys!() { |key] = delete(key)| ... } click to toggle source

The #transform_keys! method transforms all of the calling hash’s keys in accordance with the given block. The #transform_keys! method is destructive; the original hash will be changed in place after it is called.

If no block is given, the method returns an enumerator.

Example:

hash = { :foo => 'bar' }
hash.transform_keys! {|key| key.to_s.upcase }     # => { 'FOO' => 'bar' }
hash.transform_keys!                              # => #<Enumerator: {:foo=>"bar"}:transform_keys!>
hash                                              # => { 'FOO' => 'bar' }
    # File lib/reactive_support/core_ext/hash/keys.rb
145 def transform_keys!(&block)
146   return enum_for(:transform_keys) unless block_given?
147 
148   keys.each do |key|
149     self[yield key] = delete(key)
150   end
151 
152   self
153 end