Class: Hash

Inherits:
Object show all
Defined in:
lib/backports/rails/hash.rb,
lib/backports/2.3.0/hash/gt.rb,
lib/backports/2.3.0/hash/lt.rb,
lib/backports/2.3.0/hash/dig.rb,
lib/backports/2.3.0/hash/gte.rb,
lib/backports/2.3.0/hash/lte.rb,
lib/backports/1.8.7/hash/hash.rb,
lib/backports/2.0.0/hash/to_h.rb,
lib/backports/2.6.0/hash/to_h.rb,
lib/backports/1.9.1/hash/assoc.rb,
lib/backports/2.5.0/hash/slice.rb,
lib/backports/2.6.0/hash/merge.rb,
lib/backports/1.9.1/hash/rassoc.rb,
lib/backports/1.9.2/hash/select.rb,
lib/backports/3.0.0/hash/except.rb,
lib/backports/force/hash_select.rb,
lib/backports/1.9.2/hash/keep_if.rb,
lib/backports/2.3.0/hash/to_proc.rb,
lib/backports/2.4.0/hash/compact.rb,
lib/backports/2.5.0/enumerable/any.rb,
lib/backports/1.9.1/hash/default_proc.rb,
lib/backports/2.0.0/hash/default_proc.rb,
lib/backports/2.3.0/hash/fetch_values.rb,
lib/backports/2.5.0/hash/transform_keys.rb,
lib/backports/3.0.0/hash/transform_keys.rb,
lib/backports/2.4.0/hash/transform_values.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/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/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/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

#<(hash) ⇒ Object


4
5
6
7
8
9
10
11
12
# File 'lib/backports/2.3.0/hash/lt.rb', line 4

def <(hash)
  hash = Backports.coerce_to_hash(hash)
  return false unless size < hash.size
  each do |k, v|
    v2 = hash.fetch(k) { return false }
    return false unless v2 == v
  end
  true
end

#<=(hash) ⇒ Object


4
5
6
7
8
9
10
11
12
# File 'lib/backports/2.3.0/hash/lte.rb', line 4

def <=(hash)
  hash = Backports.coerce_to_hash(hash)
  return false unless size <= hash.size
  each do |k, v|
    v2 = hash.fetch(k) { return false }
    return false unless v2 == v
  end
  true
end

#>(hash) ⇒ Object


5
6
7
8
# File 'lib/backports/2.3.0/hash/gt.rb', line 5

def >(hash)
  hash = Backports.coerce_to_hash(hash)
  hash < self
end

#>=(hash) ⇒ Object


5
6
7
8
# File 'lib/backports/2.3.0/hash/gte.rb', line 5

def >=(hash)
  hash = Backports.coerce_to_hash(hash)
  hash <= self
end

#any_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
38
39
# File 'lib/backports/2.5.0/enumerable/any.rb', line 35

def any_with_pattern?(pattern = Backports::Undefined, &block)
  return any_without_pattern?(&block) if Backports::Undefined == pattern
  each_entry { |x| return true if pattern === x }
  false
end

#assoc(key) ⇒ Object


3
4
5
6
7
8
9
10
# 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

#compactObject


2
3
4
5
6
7
8
# File 'lib/backports/2.4.0/hash/compact.rb', line 2

def compact
  h = {}
  each do |key, value|
    h[key] = value unless value == nil
  end
  h
end

#compact!Object


10
11
12
# File 'lib/backports/2.4.0/hash/compact.rb', line 10

def compact!
  reject! {|_key, value| value == nil}
end

#default_proc=(proc) ⇒ Object


5
6
7
8
9
10
11
12
# 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) ⇒ Object


6
7
8
9
10
11
12
13
# 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

#dig(key, *rest) ⇒ Object

Raises:

  • (TypeError)

3
4
5
6
7
8
# File 'lib/backports/2.3.0/hash/dig.rb', line 3

def dig(key, *rest)
  val = self[key]
  return val if rest.empty? || val == nil
  raise TypeError, "#{val.class} does not have #dig method" unless val.respond_to? :dig
  val.dig(*rest)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

10
11
12
13
14
15
16
# 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

#except(*keys) ⇒ Object


2
3
4
5
6
7
8
9
# File 'lib/backports/3.0.0/hash/except.rb', line 2

def except(*keys)
  if keys.size > 4 && size > 4 # index if O(m*n) is big
    h = {}
    keys.each { |key| h[key] = true }
    keys = h
  end
  reject { |key, _value| keys.include? key}
end

#fetch_values(*keys, &block) ⇒ Object


3
4
5
6
7
# File 'lib/backports/2.3.0/hash/fetch_values.rb', line 3

def fetch_values(*keys, &block)
  keys.map do |k|
    fetch(k, &block)
  end
end

#hashObject


2
3
4
5
6
7
8
# 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_ifObject


3
4
5
6
# 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

#merge_with_backports(first = {}, *others, &block) ⇒ Object


5
6
7
8
# File 'lib/backports/2.6.0/hash/merge.rb', line 5

def merge_with_backports(first = {}, *others, &block)
  merge_without_backports(first, &block).
    merge!(*others, &block)
end

#merge_with_backports!(*hashes, &block) ⇒ Object


13
14
15
16
17
18
# File 'lib/backports/2.6.0/hash/merge.rb', line 13

def merge_with_backports!(*hashes, &block)
  hashes.each do |h|
    merge_without_backports!(h, &block)
  end
  self
end

#rassoc(value) ⇒ Object


3
4
5
6
7
# 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) ⇒ Object

Standard in rails. See official documentation


7
8
9
# File 'lib/backports/rails/hash.rb', line 7

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

#reverse_merge!(other_hash) ⇒ Object

Standard in rails. See official documentation


12
13
14
# File 'lib/backports/rails/hash.rb', line 12

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

#select!Object


3
4
5
6
7
# 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_returnObject


4
5
6
7
# File 'lib/backports/force/hash_select.rb', line 4

def select_with_hash_return
  return to_enum(:select) unless block_given?
  Hash[select_without_hash_return{|k, v| yield [k, v]}]
end

#slice(*keys) ⇒ Object


2
3
4
5
6
# File 'lib/backports/2.5.0/hash/slice.rb', line 2

def slice(*keys)
  h = {}
  keys.each { |k| h[k] = self[k] if key?(k) }
  h
end

#stringify_keysObject

Standard in rails. See official documentation


27
28
29
# File 'lib/backports/rails/hash.rb', line 27

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

#stringify_keys!Object

Standard in rails. See official documentation


32
33
34
# File 'lib/backports/rails/hash.rb', line 32

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

#symbolize_keysObject

Standard in rails. See official documentation


17
18
19
# File 'lib/backports/rails/hash.rb', line 17

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

#symbolize_keys!Object

Standard in rails. See official documentation


22
23
24
# File 'lib/backports/rails/hash.rb', line 22

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

#to_hObject


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

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

#to_h_with_block(&block) ⇒ Object


8
9
10
11
# File 'lib/backports/2.6.0/hash/to_h.rb', line 8

def to_h_with_block(&block)
  return to_h_without_block unless block
  map(&block).to_h
end

#to_procObject


3
4
5
6
# File 'lib/backports/2.3.0/hash/to_proc.rb', line 3

def to_proc
  h = self
  proc {|*args| h[*args]}
end

#transform_keysObject


2
3
4
5
6
7
8
9
# File 'lib/backports/2.5.0/hash/transform_keys.rb', line 2

def transform_keys
  return to_enum(:transform_keys) { size } unless block_given?
  h = {}
  each do |key, value|
    h[yield key] = value
  end
  h
end

#transform_keys!Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/backports/2.5.0/hash/transform_keys.rb', line 11

def transform_keys!
  return enum_for(:transform_keys!) { size } unless block_given?

  self[:trigger_error] = :immediately if frozen?

  h = {}
  begin
    each do |key, value|
      h[yield key] = value
    end
  ensure
    replace(h)
  end
  self
end

#transform_keys_with_hash_arg(hash = not_given = true, &block) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/backports/3.0.0/hash/transform_keys.rb', line 6

def transform_keys_with_hash_arg(hash = not_given = true, &block)
  return to_enum(:transform_keys) { size } if not_given && !block

  return transform_keys_without_hash_arg(&block) if not_given

  h = {}
  if block_given?
    each do |key, value|
      h[hash.fetch(key) { yield key }] = value
    end
  else
    each do |key, value|
      h[hash.fetch(key, key)] = value
    end
  end
  h
end

#transform_keys_with_hash_arg!(hash = not_given = true, &block) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backports/3.0.0/hash/transform_keys.rb', line 25

def transform_keys_with_hash_arg!(hash = not_given = true, &block)
  return enum_for(:transform_keys!) { size } if not_given && !block

  return transform_keys_without_hash_arg!(&block) if not_given

  h = {}
  begin
    if block_given?
      each do |key, value|
        h[hash.fetch(key) { yield key }] = value
      end
    else
      each do |key, value|
        h[hash.fetch(key, key)] = value
      end
    end
  ensure
    replace(h)
  end
  self
end

#transform_valuesObject


2
3
4
5
6
7
8
9
# File 'lib/backports/2.4.0/hash/transform_values.rb', line 2

def transform_values
  return to_enum(:transform_values) { size } unless block_given?
  h = {}
  each do |key, value|
    h[key] = yield value
  end
  h
end

#transform_values!Object


11
12
13
14
15
16
17
# File 'lib/backports/2.4.0/hash/transform_values.rb', line 11

def transform_values!
  return to_enum(:transform_values!) { size } unless block_given?
  reject!{} if frozen? # Force error triggerring if frozen, in case of empty array
  each do |key, value|
    self[key] = yield value
  end
end