Class: CowProxy::Hash

Inherits:
WrapClass
  • Object
show all
Includes:
Indexable, Enumerable
Defined in:
lib/cow_proxy/hash.rb

Overview

Wrapper class for Hash

Instance Method Summary collapse

Methods included from Indexable

#[], #dig, #initialize

Instance Method Details

#each {|pair| ... } ⇒ CowProxy::Hash, Enumerator Also known as: each_pair

Calls block once for each key in hash, passing the key-value pair as parameters.

Yields:

  • (pair)

    Gives each key-value pair in self to the block

Yield Parameters:

  • pair

    Array of key and wrapped value

Returns:

  • (CowProxy::Hash)

    self if block given

  • (Enumerator)

    if no block given



15
16
17
18
19
20
# File 'lib/cow_proxy/hash.rb', line 15

def each
  return enum_for(:each) unless block_given?
  __getobj__.each_key do |k|
    yield [k, self[k]]
  end
end

#each_value {|value| ... } ⇒ CowProxy::Hash, Enumerator

Calls block once for each key in hash, passing the value as parameter.

Yields:

  • (value)

    Gives each value in hash to the block

Yield Parameters:

  • value

    Wrapped value

Returns:

  • (CowProxy::Hash)

    self if block given

  • (Enumerator)

    if no block given



29
30
31
32
# File 'lib/cow_proxy/hash.rb', line 29

def each_value
  return enum_for(:each) unless block_given?
  each { |_, v| yield v }
end

#hashIntenger

Compute a hash-code for this hash. Two hashes with the same content will have the same hash code (and will compare using eql?).

Returns:

  • (Intenger)

    calculated hash code



82
83
84
# File 'lib/cow_proxy/hash.rb', line 82

def hash
  __getobj__.hash
end

#include?(key) ⇒ Array

Returns true if the given key is present in hash.

Returns:

  • (Array)

    Wrapped values from hash



66
67
68
# File 'lib/cow_proxy/hash.rb', line 66

def include?(key)
  key?(key)
end

#reject {|pair| ... } ⇒ CowProxy::Hash, Enumerator

Returns a new hash consisting of entries for which the block returns false.

Yields:

  • (pair)

    Gives each key-value pair in self to the block

Yield Parameters:

  • pair

    Array of key and wrapped value

Yield Returns:

  • (Boolean)

    true if item must not be included

Returns:

  • (CowProxy::Hash)

    self if block given

  • (Enumerator)

    if no block given



52
53
54
# File 'lib/cow_proxy/hash.rb', line 52

def reject
  ::Hash[super]
end

#select {|pair| ... } ⇒ CowProxy::Hash, Enumerator

Returns a new hash consisting of entries for which the block returns true.

Yields:

  • (pair)

    Gives each key-value pair in self to the block

Yield Parameters:

  • pair

    Array of key and wrapped value

Yield Returns:

  • (Boolean)

    true if item must be included

Returns:

  • (CowProxy::Hash)

    self if block given

  • (Enumerator)

    if no block given



41
42
43
# File 'lib/cow_proxy/hash.rb', line 41

def select
  ::Hash[super]
end

#to_hashHash

Used for merging into another Hash needs to return unwrapped Hash

Returns:

  • (Hash)

    wrapped object



74
75
76
# File 'lib/cow_proxy/hash.rb', line 74

def to_hash
  __getobj__
end

#valuesArray

Returns a new array populated with the wrapped values from hash.

Returns:

  • (Array)

    Wrapped values from hash



59
60
61
# File 'lib/cow_proxy/hash.rb', line 59

def values
  map(&:last)
end