Sha256: 52113c9d91a430661db29f4140370ad7568123ce391193024a048e3edc447cbd

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

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

  # Merges the caller into +other_hash+. For example,
  #
  #   options = options.reverse_merge(size: 25, velocity: 10)
  #
  # is equivalent to
  #
  #   options = { size: 25, velocity: 10 }.merge(options)
  #
  # This is particularly useful for initializing an options hash
  # with default values.
  def reverse_merge(other_hash)
    other_hash.merge(self)
  end

  # Destructive +reverse_merge+.
  def reverse_merge!(other_hash)
    # right wins if there is no left
    merge!(other_hash) { |key,left,right| left }
  end

end

Version data entries

14 entries across 14 versions & 3 rubygems

Version Path
agile_utils-0.0.9 lib/agile_utils/core_ext/hash/hash.rb
agile_utils-0.0.8 lib/agile_utils/core_ext/hash/hash.rb
agile_utils-0.0.7 lib/agile_utils/core_ext/hash/hash.rb
agile_utils-0.0.6 lib/agile_utils/core_ext/hash/hash.rb
ebook_renamer-0.1.2 lib/ebook_renamer/core_ext/hash.rb
vim_printer-0.0.5 lib/vim_printer/core_ext/hash.rb
agile_utils-0.0.5 lib/agile_utils/core_ext/hash/hash.rb
vim_printer-0.0.4 lib/vim_printer/core_ext/hash.rb
agile_utils-0.0.4 lib/agile_utils/core_ext/hash/hash.rb
vim_printer-0.0.3 lib/vim_printer/core_ext/hash.rb
agile_utils-0.0.2 lib/agile_utils/core_ext/hash/hash.rb
vim_printer-0.0.2 lib/vim_printer/core_ext/hash.rb
vim_printer-0.0.1 lib/vim_printer/core_ext/hash.rb
ebook_renamer-0.1.1 lib/ebook_renamer/core_ext/hash.rb