class Cistern::Hash

Public Class Methods

except(hash, *keys) click to toggle source

@example

Cistern::Hash.except({ :a => 1, :b => 2 }, :a) #=> { :b => 2 }

@return [Hash] copy of {#hash} containing all keys except {#keys}

# File lib/cistern/hash.rb, line 14
def self.except(hash, *keys)
  Cistern::Hash.except!(hash.dup, *keys)
end
except!(hash, *keys) click to toggle source

Remove all keys not specified in {#keys} from {#hash} in place

@example

Cistern::Hash.except({ :a => 1, :b => 2 }, :a) #=> { :b => 2 }

@return [Hash] {#hash} @see {Cistern::Hash#except}

# File lib/cistern/hash.rb, line 24
def self.except!(hash, *keys)
  keys.each { |key| hash.delete(key) }
  hash
end
slice(hash, *keys) click to toggle source

@example

Cistern::Hash.slice({ :a => 1, :b => 2 }, :a) #=> { :a => 1 }

@return [Hash] copy of {#hash} containing only {#keys}

# File lib/cistern/hash.rb, line 7
def self.slice(hash, *keys)
  keys.each_with_object({}) { |e, a| a[e] = hash[e] if hash.key?(e) }
end
stringify_keys(object) click to toggle source

Copy {#hash} and convert all keys to strings recursively.

@example

Cistern::Hash.stringify_keys(:a => 1, :b => 2) #=> { 'a' => 1, 'b' => 2 }

@return [Hash] {#hash} with string keys

# File lib/cistern/hash.rb, line 34
def self.stringify_keys(object)
  case object
  when Hash
    object.each_with_object({}) { |(k, v), a| a[k.to_s] = stringify_keys(v) }
  when Array
    object.map { |v| stringify_keys(v) }
  else
    object
  end
end