Sha256: d38b072a16376e485a6548ab82d45c6a3023f74f13ba86a320d67add72e7d038

Contents?: true

Size: 856 Bytes

Versions: 14

Compression:

Stored size: 856 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

14 entries across 14 versions & 2 rubygems

Version Path
index_html-0.0.8 lib/active_support/core_ext/hash/hash.rb
code_lister-0.0.7 lib/code_lister/core_ext/hash.rb
index_html-0.0.7 lib/active_support/core_ext/hash/hash.rb
index_html-0.0.6 lib/active_support/core_ext/hash/hash.rb
index_html-0.0.5 lib/active_support/core_ext/hash/hash.rb
index_html-0.0.4 lib/active_support/core_ext/hash/hash.rb
code_lister-0.0.6 lib/code_lister/core_ext/hash.rb
index_html-0.0.3 lib/active_support/core_ext/hash/hash.rb
code_lister-0.0.5 lib/code_lister/core_ext/hash.rb
index_html-0.0.2 lib/active_support/core_ext/hash/hash.rb
index_html-0.0.1 lib/active_support/core_ext/hash/hash.rb
code_lister-0.0.4 lib/code_lister/core_ext/hash.rb
code_lister-0.0.3 lib/code_lister/core_ext/hash.rb
code_lister-0.0.2 lib/code_lister/core_ext/hash.rb