class Hash - activesupport-4.2.3 Documentation

# class Hash

### Public Class Methods

from_trusted_xml(xml) click to toggle source

Builds a Hash from XML just like `Hash.from_xml`, but also allows Symbol and YAML.

    # File lib/active_support/core_ext/hash/conversions.rb, line 115
    def from_trusted_xml(xml)
      from_xml xml, []
    end

from_xml(xml, disallowed_types = nil) click to toggle source

Returns a Hash containing a collection of pairs when the key is the node name and the value is its content

    xml = <<-XML
      <?xml version="1.0" encoding="UTF-8"?>
        <hash>
          <foo type="integer">1</foo>
          <bar type="integer">2</bar>
        </hash>
    XML

    hash = Hash.from_xml(xml)
    # => {"hash"=>{"foo"=>1, "bar"=>2}}

`DisallowedType` is raised if the XML contains attributes with `type="yaml"` or `type="symbol"`. Use `Hash.from_trusted_xml` to parse this XML.

    # File lib/active_support/core_ext/hash/conversions.rb, line 110
    def from_xml(xml, disallowed_types = nil)
      ActiveSupport::XMLConverter.new(xml, disallowed_types).to_h
    end

### Public Instance Methods

assert_valid_keys(*valid_keys) click to toggle source

Validate all keys in a hash match `*valid_keys`, raising ArgumentError on a mismatch.

Note that keys are treated differently than HashWithIndifferentAccess, meaning that string and symbol keys will not match.

    { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
    { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
    { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age)   # => passes, raises nothing

    # File lib/active_support/core_ext/hash/keys.rb, line 71
    def assert_valid_keys(*valid_keys)
      valid_keys.flatten!
      each_key do |k|
        unless valid_keys.include?(k)
          raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
        end
      end
    end

compact() click to toggle source

Returns a hash with non `nil` values.

    hash = { a: true, b: false, c: nil}
    hash.compact # => { a: true, b: false}
    hash # => { a: true, b: false, c: nil}
    { c: nil }.compact # => {}

    # File lib/active_support/core_ext/hash/compact.rb, line 8
    def compact
      self.select { |_, value| !value.nil? }
    end

compact!() click to toggle source

Replaces current hash with non `nil` values.

    hash = { a: true, b: false, c: nil}
    hash.compact! # => { a: true, b: false}
    hash # => { a: true, b: false}

    # File lib/active_support/core_ext/hash/compact.rb, line 17
    def compact!
      self.reject! { |_, value| value.nil? }
    end

deep_dup() click to toggle source

Returns a deep copy of hash.

    hash = { a: { b: 'b' } }
    dup  = hash.deep_dup
    dup[:a][:c] = 'c'

    hash[:a][:c] # => nil
    dup[:a][:c]  # => "c"

    # File lib/active_support/core_ext/object/deep_dup.rb, line 41
    def deep_dup
      each_with_object(dup) do |(key, value), hash|
        hash[key.deep_dup] = value.deep_dup
      end
    end

deep_merge(other_hash, &block) click to toggle source

Returns a new hash with `self` and `other_hash` merged recursively.

    h1 = { a: true, b: { c: [1, 2, 3] } }
    h2 = { a: false, b: { x: [3, 4, 5] } }

    h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }

Like with Hash#merge in the standard library, a block can be provided to merge values:

    h1 = { a: 100, b: 200, c: { c1: 100 } }
    h2 = { b: 250, c: { c1: 200 } }
    h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
    # => { a: 100, b: 450, c: { c1: 300 } }

    # File lib/active_support/core_ext/hash/deep_merge.rb, line 16
    def deep_merge(other_hash, &block)
      dup.deep_merge!(other_hash, &block)
    end

deep_merge!(other_hash, &block) click to toggle source

Same as `deep_merge`, but modifies `self`.

    # File lib/active_support/core_ext/hash/deep_merge.rb, line 21
    def deep_merge!(other_hash, &block)
      other_hash.each_pair do |current_key, other_value|
        this_value = self[current_key]

        self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
          this_value.deep_merge(other_value, &block)
        else
          if block_given? && key?(current_key)
            block.call(current_key, this_value, other_value)
          else
            other_value
          end
        end
      end

      self
    end

deep_stringify_keys() click to toggle source

Returns a new hash with all keys converted to strings. This includes the keys from the root hash and from all nested hashes and arrays.

    hash = { person: { name: 'Rob', age: '28' } }

    hash.deep_stringify_keys
    # => {"person"=>{"name"=>"Rob", "age"=>"28"}}

    # File lib/active_support/core_ext/hash/keys.rb, line 107
    def deep_stringify_keys
      deep_transform_keys{ |key| key.to_s }
    end

deep_stringify_keys!() click to toggle source

Destructively convert all keys to strings. This includes the keys from the root hash and from all nested hashes and arrays.

    # File lib/active_support/core_ext/hash/keys.rb, line 114
    def deep_stringify_keys!
      deep_transform_keys!{ |key| key.to_s }
    end

deep_symbolize_keys() click to toggle source

Returns a new hash with all keys converted to symbols, as long as they respond to `to_sym`. This includes the keys from the root hash and from all nested hashes and arrays.

    hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }

    hash.deep_symbolize_keys
    # => {:person=>{:name=>"Rob", :age=>"28"}}

    # File lib/active_support/core_ext/hash/keys.rb, line 126
    def deep_symbolize_keys
      deep_transform_keys{ |key| key.to_sym rescue key }
    end

deep_symbolize_keys!() click to toggle source

Destructively convert all keys to symbols, as long as they respond to `to_sym`. This includes the keys from the root hash and from all nested hashes and arrays.

    # File lib/active_support/core_ext/hash/keys.rb, line 133
    def deep_symbolize_keys!
      deep_transform_keys!{ |key| key.to_sym rescue key }
    end

deep_transform_keys(&block) click to toggle source

Returns a new hash with all keys converted by the block operation. This includes the keys from the root hash and from all nested hashes and arrays.

    hash = { person: { name: 'Rob', age: '28' } }

    hash.deep_transform_keys{ |key| key.to_s.upcase }
    # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}

    # File lib/active_support/core_ext/hash/keys.rb, line 88
    def deep_transform_keys(&block)
      _deep_transform_keys_in_object(self, &block)
    end

deep_transform_keys!(&block) click to toggle source

Destructively convert all keys by using the block operation. This includes the keys from the root hash and from all nested hashes and arrays.

    # File lib/active_support/core_ext/hash/keys.rb, line 95
    def deep_transform_keys!(&block)
      _deep_transform_keys_in_object!(self, &block)
    end

except(*keys) click to toggle source

Returns a hash that includes everything but the given keys.

    hash = { a: true, b: false, c: nil}
    hash.except(:c) # => { a: true, b: false}
    hash # => { a: true, b: false, c: nil}

This is useful for limiting a set of parameters to everything but a few known toggles:

    @person.update(params[:person].except(:admin))

    # File lib/active_support/core_ext/hash/except.rb, line 9
    def except(*keys)
      dup.except!(*keys)
    end

except!(*keys) click to toggle source

Replaces the hash without the given keys.

    hash = { a: true, b: false, c: nil}
    hash.except!(:c) # => { a: true, b: false}
    hash # => { a: true, b: false }

    # File lib/active_support/core_ext/hash/except.rb, line 17
    def except!(*keys)
      keys.each { |key| delete(key) }
      self
    end

extract!(*keys) click to toggle source

Removes and returns the key/value pairs matching the given keys.

    { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
    { a: 1, b: 2 }.extract!(:a, :x)             # => {:a=>1}

    # File lib/active_support/core_ext/hash/slice.rb, line 45
    def extract!(*keys)
      keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
    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 as extractable. If a Hash is extractable, Array#extract_options! pops it from the Array when it is the last element of the Array.

    # File lib/active_support/core_ext/array/extract_options.rb, line 7
    def extractable_options?
      instance_of?(Hash)
    end

nested_under_indifferent_access()

Called when object is nested under an object that receives with_indifferent_access. This method will be called on the current object by the enclosing object and is aliased to with_indifferent_access by default. Subclasses of Hash may overwrite this method to return `self` if converting to an `ActiveSupport::HashWithIndifferentAccess` would not be desirable.

    b = { b: 1 }
    { a: b }.with_indifferent_access['a'] # calls b.nested_under_indifferent_access
    # => {"b"=>1}

Alias for: with_indifferent_access
reverse_merge(other_hash) click to toggle source

Merges the caller into `other_hash`. For example,

    options = options.reverse_merge(size: 25, velocity: 10)

is equivalent to

    options = { size: 25, velocity: 10 }.merge(options)

This is particularly useful for initializing an options hash with default values.

    # File lib/active_support/core_ext/hash/reverse_merge.rb, line 12
    def reverse_merge(other_hash)
      other_hash.merge(self)
    end

reverse_merge!(other_hash) click to toggle source

Destructive `reverse_merge`.

    # File lib/active_support/core_ext/hash/reverse_merge.rb, line 17
    def reverse_merge!(other_hash)
      # right wins if there is no left
      merge!( other_hash ){|key,left,right| left }
    end

Also aliased as: reverse_update
reverse_update(other_hash)
Alias for: reverse_merge!
slice(*keys) click to toggle source

Slice a hash to include only the given keys. Returns a hash containing the given keys.

    { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
    # => {:a=>1, :b=>2}

This is useful for limiting an options hash to valid keys before passing to a method:

    def search(criteria = {})
      criteria.assert_valid_keys(:mass, :velocity, :time)
    end

    search(options.slice(:mass, :velocity, :time))

If you have an array of keys you want to limit to, you should splat them:

    valid_keys = [:mass, :velocity, :time]
    search(options.slice(*valid_keys))

    # File lib/active_support/core_ext/hash/slice.rb, line 21
    def slice(*keys)
      keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
      keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
    end

slice!(*keys) click to toggle source

Replaces the hash with only the given keys. Returns a hash containing the removed key/value pairs.

    { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
    # => {:c=>3, :d=>4}

    # File lib/active_support/core_ext/hash/slice.rb, line 31
    def slice!(*keys)
      keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
      omit = slice(*self.keys - keys)
      hash = slice(*keys)
      hash.default      = default
      hash.default_proc = default_proc if default_proc
      replace(hash)
      omit
    end

stringify_keys() click to toggle source

Returns a new hash with all keys converted to strings.

    hash = { name: 'Rob', age: '28' }

    hash.stringify_keys
    # => {"name"=>"Rob", "age"=>"28"}

    # File lib/active_support/core_ext/hash/keys.rb, line 33
    def stringify_keys
      transform_keys{ |key| key.to_s }
    end

stringify_keys!() click to toggle source

Destructively convert all keys to strings. Same as `stringify_keys`, but modifies `self`.

    # File lib/active_support/core_ext/hash/keys.rb, line 39
    def stringify_keys!
      transform_keys!{ |key| key.to_s }
    end

symbolize_keys() click to toggle source

Returns a new hash with all keys converted to symbols, as long as they respond to `to_sym`.

    hash = { 'name' => 'Rob', 'age' => '28' }

    hash.symbolize_keys
    # => {:name=>"Rob", :age=>"28"}

    # File lib/active_support/core_ext/hash/keys.rb, line 50
    def symbolize_keys
      transform_keys{ |key| key.to_sym rescue key }
    end

Also aliased as: to_options
symbolize_keys!() click to toggle source

Destructively convert all keys to symbols, as long as they respond to `to_sym`. Same as `symbolize_keys`, but modifies `self`.

    # File lib/active_support/core_ext/hash/keys.rb, line 57
    def symbolize_keys!
      transform_keys!{ |key| key.to_sym rescue key }
    end

Also aliased as: to_options!
to_options()
Alias for: symbolize_keys
to_options!()
Alias for: symbolize_keys!
to_param(namespace = nil)
Alias for: to_query
to_query(namespace = nil) click to toggle source

Returns a string representation of the receiver suitable for use as a URL query string:

    {name: 'David', nationality: 'Danish'}.to_query
    # => "name=David&nationality=Danish"

An optional namespace can be passed to enclose key names:

    {name: 'David', nationality: 'Danish'}.to_query('user')
    # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"

The string pairs “key=value” that conform the query string are sorted lexicographically in ascending order.

This method is also aliased as `to_param`.

    # File lib/active_support/core_ext/object/to_query.rb, line 75
    def to_query(namespace = nil)
      collect do |key, value|
        unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
          value.to_query(namespace ? "#{namespace}[#{key}]" : key)
        end
      end.compact.sort! * '&'
    end

Also aliased as: to_param
to_xml(options = {}) { |builder| ... } click to toggle source

Returns a string containing an XML representation of its receiver:

    { foo: 1, bar: 2 }.to_xml
    # =>
    # <?xml version="1.0" encoding="UTF-8"?>
    # <hash>
    #   <foo type="integer">1</foo>
    #   <bar type="integer">2</bar>
    # </hash>

To do so, the method loops over the pairs and builds nodes that depend on the values. Given a pair `key`, `value`:

- If `value` is a hash there's a recursive call with `key` as `:root`.

- If `value` is an array there's a recursive call with `key` as `:root`, and `key` singularized as `:children`.

- If `value` is a callable object it must expect one or two arguments. Depending on the arity, the callable is invoked with the `options` hash as first argument with `key` as `:root`, and `key` singularized as second argument. The callable can add nodes by using `options[:builder]`.

      'foo'.to_xml(lambda { |options, key| options[:builder].b(key) })
      # => "<b>foo</b>"

- If `value` responds to `to_xml` the method is invoked with `key` as `:root`.

      class Foo
        def to_xml(options)
          options[:builder].bar 'fooing!'
        end
      end

      { foo: Foo.new }.to_xml(skip_instruct: true)
      # =>
      # <hash>
      #   <bar>fooing!</bar>
      # </hash>

- Otherwise, a node with `key` as tag is created with a string representation of `value` as text node. If `value` is `nil` an attribute “nil” set to “true” is added. Unless the option `:skip_types` exists and is true, an attribute “type” is added as well according to the following mapping:

      XML_TYPE_NAMES = {
        "Symbol"     => "symbol",
        "Fixnum"     => "integer",
        "Bignum"     => "integer",
        "BigDecimal" => "decimal",
        "Float"      => "float",
        "TrueClass"  => "boolean",
        "FalseClass" => "boolean",
        "Date"       => "date",
        "DateTime"   => "dateTime",
        "Time"       => "dateTime"
      }

By default the root node is “hash”, but that's configurable via the `:root` option.

The default XML builder is a fresh instance of `Builder::XmlMarkup`. You can configure your own builder with the `:builder` option. The method also accepts options like `:dasherize` and friends, they are forwarded to the builder.

    # File lib/active_support/core_ext/hash/conversions.rb, line 74
    def to_xml(options = {})
      require 'active_support/builder' unless defined?(Builder)

      options = options.dup
      options[:indent]  ||= 2
      options[:root]    ||= 'hash'
      options[:builder] ||= Builder::XmlMarkup.new(indent: options[:indent])

      builder = options[:builder]
      builder.instruct! unless options.delete(:skip_instruct)

      root = ActiveSupport::XmlMini.rename_key(options[:root].to_s, options)

      builder.tag!(root) do
        each { |key, value| ActiveSupport::XmlMini.to_tag(key, value, options) }
        yield builder if block_given?
      end
    end

transform_keys() { |key| ... } click to toggle source

Returns a new hash with all keys converted using the block operation.

    hash = { name: 'Rob', age: '28' }

    hash.transform_keys{ |key| key.to_s.upcase }
    # => {"NAME"=>"Rob", "AGE"=>"28"}

    # File lib/active_support/core_ext/hash/keys.rb, line 8
    def transform_keys
      return enum_for(:transform_keys) unless block_given?
      result = self.class.new
      each_key do |key|
        result[yield(key)] = self[key]
      end
      result
    end

transform_keys!() { |key| ... } click to toggle source

Destructively convert all keys using the block operations. Same as #transform_keys but modifies `self`.

    # File lib/active_support/core_ext/hash/keys.rb, line 19
    def transform_keys!
      return enum_for(:transform_keys!) unless block_given?
      keys.each do |key|
        self[yield(key)] = delete(key)
      end
      self
    end

transform_values() { |value| ... } click to toggle source

Returns a new hash with the results of running `block` once for every value. The keys are unchanged.

    { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 }
    # => { a: 2, b: 4, c: 6 }

    # File lib/active_support/core_ext/hash/transform_values.rb, line 7
    def transform_values
      return enum_for(:transform_values) unless block_given?
      result = self.class.new
      each do |key, value|
        result[key] = yield(value)
      end
      result
    end

transform_values!() { |value| ... } click to toggle source

Destructive `transform_values`

    # File lib/active_support/core_ext/hash/transform_values.rb, line 17
    def transform_values!
      return enum_for(:transform_values!) unless block_given?
      each do |key, value|
        self[key] = yield(value)
      end
    end

with_indifferent_access() click to toggle source

Returns an `ActiveSupport::HashWithIndifferentAccess` out of its receiver:

    { a: 1 }.with_indifferent_access['a'] # => 1

    # File lib/active_support/core_ext/hash/indifferent_access.rb, line 8
    def with_indifferent_access
      ActiveSupport::HashWithIndifferentAccess.new_from_hash_copying_default(self)
    end

Also aliased as: nested_under_indifferent_access

### Private Instance Methods

_deep_transform_keys_in_object(object) { |key| ... } click to toggle source

support methods for deep transforming nested hashes and arrays

    # File lib/active_support/core_ext/hash/keys.rb, line 139
    def _deep_transform_keys_in_object(object, &block)
      case object
      when Hash
        object.each_with_object({}) do |(key, value), result|
          result[yield(key)] = _deep_transform_keys_in_object(value, &block)
        end
      when Array
        object.map {|e| _deep_transform_keys_in_object(e, &block) }
      else
        object
      end
    end

_deep_transform_keys_in_object!(object) { |key| ... } click to toggle source

    # File lib/active_support/core_ext/hash/keys.rb, line 152
    def _deep_transform_keys_in_object!(object, &block)
      case object
      when Hash
        object.keys.each do |key|
          value = object.delete(key)
          object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
        end
        object
      when Array
        object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
      else
        object
      end
    end