class Hash

Public Class Methods

[](*args) click to toggle source
# File lib/backports/1.8.7/hash/constructor.rb, line 6
def [](*args)
  if args.length == 1
    arg = args.first
    if (h = Backports.try_convert(arg, Hash, :to_hash))
      return allocate.replace(h)
    end
    if (kvps = Backports.is_array?(arg))
      h = {}
      kvps.each do |elem|
        next unless arr = Backports.is_array?(elem)
        next unless (1..2).include? arr.size
        h[arr.at(0)] = arr.at(1)
      end
      return h
    end
  end
  constructor_without_key_value_pair_form(*args)
end
constructor_without_key_value_pair_form(*args)
Alias for: []
try_convert(x) click to toggle source
# File lib/backports/1.9.1/hash/try_convert.rb, line 4
def Hash.try_convert(x)
  Backports.try_convert(x, Hash, :to_hash)
end

Public Instance Methods

assoc(key) click to toggle source
# File lib/backports/1.9.1/hash/assoc.rb, line 3
def assoc(key)
  val = fetch(key) do
    return find do |k, v|
      [k, v] if k == key
    end
  end
  [key, val]
end
default_proc=(proc) click to toggle source
# File lib/backports/1.9.1/hash/default_proc.rb, line 5
def default_proc=(proc)
  if proc == nil # nil accepted in Ruby 2.0
    self.default = nil
    self
  else
    replace(Hash.new(&Backports.coerce_to(proc, Proc, :to_proc)).merge!(self))
  end
end
default_proc_with_nil=(proc) click to toggle source
# File lib/backports/2.0.0/hash/default_proc.rb, line 6
def default_proc_with_nil=(proc)
  if proc == nil
    self.default = nil
    self
  else
    self.default_proc_without_nil=(proc)
  end
end
eql?(other) click to toggle source
# File lib/backports/1.8.7/hash/hash.rb, line 10
def eql?(other)
  other.is_a?(Hash) &&
    size == other.size &&
    all? do |key, value|
      value.eql?(other.fetch(key){return false})
    end
end
hash() click to toggle source
# File lib/backports/1.8.7/hash/hash.rb, line 2
def hash
  h = 0
  each do |key, value|
    h ^= key.hash ^ value.hash
  end
  h
end
keep_if() { |key, value| ... } click to toggle source
# File lib/backports/1.9.2/hash/keep_if.rb, line 3
def keep_if
  return to_enum(:keep_if) unless block_given?
  delete_if{|key, value| ! yield key, value}
end
rassoc(value) click to toggle source
# File lib/backports/1.9.1/hash/rassoc.rb, line 3
def rassoc(value)
  k = key(value)
  v = fetch(k){return nil}
  [k, fetch(k)] if k || v == value
end
reverse_merge(other_hash) click to toggle source

Standard in rails. See official documentation

# File lib/backports/rails/hash.rb, line 3
def reverse_merge(other_hash)
  other_hash.merge(self)
end
reverse_merge!(other_hash) click to toggle source

Standard in rails. See official documentation

# File lib/backports/rails/hash.rb, line 8
def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end
select!() { |key, value| ... } click to toggle source
# File lib/backports/1.9.2/hash/select.rb, line 3
def select!
  return to_enum(:select!) unless block_given?
  raise "can't modify frozen hash" if frozen? # reject! won't do it for empty hashes...
  reject!{|key, value| ! yield key, value}
end
select_with_hash_return() { |k, v| ... } click to toggle source
# File lib/backports/force/hash_select.rb, line 3
def select_with_hash_return
  return to_enum(:select) unless block_given?
  Hash[select_without_hash_return{|k, v| yield [k, v]}]
end
stringify_keys() click to toggle source

Standard in rails. See official documentation

# File lib/backports/rails/hash.rb, line 23
def stringify_keys
  Hash[map{|key,value| [key.to_s, value] }]
end
stringify_keys!() click to toggle source

Standard in rails. See official documentation

# File lib/backports/rails/hash.rb, line 28
def stringify_keys!
  self.replace(self.stringify_keys)
end
symbolize_keys() click to toggle source

Standard in rails. See official documentation

# File lib/backports/rails/hash.rb, line 13
def symbolize_keys
  Hash[map{|key,value| [(key.to_sym rescue key) || key, value] }]
end
symbolize_keys!() click to toggle source

Standard in rails. See official documentation

# File lib/backports/rails/hash.rb, line 18
def symbolize_keys!
  self.replace(self.symbolize_keys)
end
to_h() click to toggle source
# File lib/backports/2.0.0/hash/to_h.rb, line 3
def to_h
  self.class == Hash ? self : {}.replace(self)
end