Sha256: 1c57c8720795bd46282cd9857a22379e41ed43004a7606efa67849a3ba275d3f

Contents?: true

Size: 857 Bytes

Versions: 1

Compression:

Stored size: 857 Bytes

Contents

class Hash
  # File activesupport/lib/active_support/core_ext/hash/keys.rb
  #
  # hash = { name: 'Rob', age: '28' }
  # hash.transform_keys{ |key| key.to_s.upcase }
  # => { "NAME" => "Rob", "AGE" => "28" }
  def transform_keys
    result = {}
    each_key do |key|
      result[yield(key)] = self[key]
    end
    result
  end

  def transform_keys!(&block)
    keys.each do |key|
      value = delete(key)
      self[yield(key)] = value.is_a?(Hash) ? value.transform_keys!(&block) : value
    end
    self
  end

  # hash = { 'name' => 'Rob', 'age' => '28' }
  # hash.symbolize_keys
  # => { name: "Rob", age: "28" }
  def symbolize_keys
    transform_keys { |key| key.to_sym rescue key }
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
extension_lister-0.1.0 lib/extension_lister/core_ext/hash/keys.rb