class Hash

Public Instance Methods

compact() click to toggle source

Returns a copy of self with all blank keys removed.

hash = { name: 'Rob', age: '', title: nil }

hash.compact
# => { name: 'Rob' }
# File lib/fir/patches/hash.rb, line 10
def compact
  delete_if { |_, v| v.is_a?(FalseClass) ? false : v.blank? }
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/fir/patches/hash.rb, line 60
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/fir/patches/hash.rb, line 48
def deep_transform_keys(&block)
  _deep_transform_keys_in_object(self, &block)
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/fir/patches/hash.rb, line 36
def symbolize_keys
  transform_keys { |key| key.to_sym rescue key }
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/fir/patches/hash.rb, line 20
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

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/fir/patches/hash.rb, line 67
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