class Hash

Public Instance Methods

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/wedge/utilis/hash.rb, line 147
def deep_dup
  each_with_object(dup) do |(key, value), hash|
    hash.delete(key)
    hash[key.deep_dup] = value.deep_dup
  end
end
deep_merge(second) click to toggle source
# File lib/wedge/utilis/hash.rb, line 25
def deep_merge(second)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  self.merge(second, &merger)
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/wedge/utilis/hash.rb, line 73
def extract!(*keys)
  keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
end
indifferent() click to toggle source
# File lib/wedge/utilis/hash.rb, line 77
def indifferent
  Wedge::IndifferentHash.new self
end
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/wedge/utilis/hash.rb, line 49
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/wedge/utilis/hash.rb, line 59
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
to_obj() click to toggle source

add keys to hash

# File lib/wedge/utilis/hash.rb, line 5
def to_obj
  self.each do |k,v|
    if v.kind_of? Hash
      v.to_obj
    end
    k=k.to_s.gsub(/\.|\s|-|\/|\'/, '_').downcase.to_sym

    ## create and initialize an instance variable for this key/value pair
    self.instance_variable_set("@#{k}", v)

    ## create the getter that returns the instance variable
    self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})

    ## create the setter that sets the instance variable
    self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
  end

  return self
end