Module: Polyfill::V2_3::Hash

Defined in:
lib/polyfill/v2_3/hash.rb

Instance Method Summary collapse

Instance Method Details

#<(other) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/polyfill/v2_3/hash.rb', line 4

def <(other)
  other = InternalUtils.to_hash(other)

  return false if size == other.size

  all? { |k, v| other[k] == v }
end

#<=(other) ⇒ Object



12
13
14
15
16
# File 'lib/polyfill/v2_3/hash.rb', line 12

def <=(other)
  other = InternalUtils.to_hash(other)

  all? { |k, v| other[k] == v }
end

#>(other) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/polyfill/v2_3/hash.rb', line 18

def >(other)
  other = InternalUtils.to_hash(other)

  return false if size == other.size

  other.all? { |k, v| self[k] == v }
end

#>=(other) ⇒ Object



26
27
28
29
30
# File 'lib/polyfill/v2_3/hash.rb', line 26

def >=(other)
  other = InternalUtils.to_hash(other)

  other.all? { |k, v| self[k] == v }
end

#dig(head, *rest) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/polyfill/v2_3/hash.rb', line 32

def dig(head, *rest)
  [head, *rest].reduce(self) do |value, accessor|
    next_value =
      case value
      when ::Array
        value.at(accessor)
      when ::Hash
        value[accessor]
      when ::Struct
        value[accessor] if value.members.include?(accessor)
      else
        begin
          break value.dig(*rest)
        rescue NoMethodError
          raise TypeError, "#{value.class} does not have a #dig method"
        end
      end

    break nil if next_value.nil?
    next_value
  end
end

#fetch_values(*keys) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/polyfill/v2_3/hash.rb', line 55

def fetch_values(*keys)
  if block_given?
    block = ::Proc.new

    keys.each_with_object([]) do |key, values|
      values << fetch(key, &block)
    end
  else
    keys.each_with_object([]) do |key, values|
      values << fetch(key)
    end
  end
end

#to_procObject



69
70
71
# File 'lib/polyfill/v2_3/hash.rb', line 69

def to_proc
  method(:[]).to_proc
end