Class: Hash

Inherits:
Object show all
Defined in:
lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb,
lib/vendor/backports-3.3.5/lib/backports/1.8.7/hash/hash.rb,
lib/vendor/backports-3.3.5/lib/backports/2.0.0/hash/to_h.rb,
lib/vendor/backports-3.3.5/lib/backports/1.9.1/hash/assoc.rb,
lib/vendor/backports-3.3.5/lib/backports/1.9.1/hash/rassoc.rb,
lib/vendor/backports-3.3.5/lib/backports/1.9.2/hash/select.rb,
lib/vendor/backports-3.3.5/lib/backports/force/hash_select.rb,
lib/vendor/backports-3.3.5/lib/backports/1.9.2/hash/keep_if.rb,
lib/vendor/backports-3.3.5/lib/backports/1.9.1/hash/default_proc.rb,
lib/vendor/backports-3.3.5/lib/backports/2.0.0/hash/default_proc.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vendor/backports-3.3.5/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_formObject



5
# File 'lib/vendor/backports-3.3.5/lib/backports/1.8.7/hash/constructor.rb', line 5

alias_method :constructor_without_key_value_pair_form, :[]

.try_convert(x) ⇒ Object



4
5
6
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/hash/try_convert.rb', line 4

def Hash.try_convert(x)
  Backports.try_convert(x, Hash, :to_hash)
end

Instance Method Details

#assoc(key) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/vendor/backports-3.3.5/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) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/vendor/backports-3.3.5/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) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/vendor/backports-3.3.5/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) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/vendor/backports-3.3.5/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

#hashObject



2
3
4
5
6
7
8
# File 'lib/vendor/backports-3.3.5/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_ifObject



3
4
5
6
# File 'lib/vendor/backports-3.3.5/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) ⇒ Object



3
4
5
6
7
# File 'lib/vendor/backports-3.3.5/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) ⇒ Object

Standard in rails. See official documentation



3
4
5
# File 'lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb', line 3

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object

Standard in rails. See official documentation



8
9
10
# File 'lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb', line 8

def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end

#select!Object



3
4
5
6
7
# File 'lib/vendor/backports-3.3.5/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_returnObject



3
4
5
6
# File 'lib/vendor/backports-3.3.5/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_keysObject

Standard in rails. See official documentation



23
24
25
# File 'lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb', line 23

def stringify_keys
  Hash[map{|key,value| [key.to_s, value] }]
end

#stringify_keys!Object

Standard in rails. See official documentation



28
29
30
# File 'lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb', line 28

def stringify_keys!
  self.replace(self.stringify_keys)
end

#symbolize_keysObject

Standard in rails. See official documentation



13
14
15
# File 'lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb', line 13

def symbolize_keys
  Hash[map{|key,value| [(key.to_sym rescue key) || key, value] }]
end

#symbolize_keys!Object

Standard in rails. See official documentation



18
19
20
# File 'lib/vendor/backports-3.3.5/lib/backports/rails/hash.rb', line 18

def symbolize_keys!
  self.replace(self.symbolize_keys)
end

#to_hObject



3
4
5
# File 'lib/vendor/backports-3.3.5/lib/backports/2.0.0/hash/to_h.rb', line 3

def to_h
  self.class == Hash ? self : {}.replace(self)
end