Module: Gorillib::Hashlike::EnumerateFromKeys

Defined in:
lib/gorillib/hashlike.rb

Overview

Provides a natural default iteration behavior by iterating over #keys. Since most classes will want this behaviour, it is included by default unless the class has already defined an #each method.

Classes that wish to define their own iteration behavior (Struct for example, or a database facade) must define all of the methods within this module.

Instance Method Summary collapse

Instance Method Details

#hsh.each {|key, val| ... } ⇒ Hashlike #hsh.each(->an_enumerator) ⇒ Enumerator

Calls +block+ once for each key in +hsh+, passing the key/value pair as parameters.

If no block is given, an enumerator is returned instead.

Examples:

hsh = { :a => 100, :b => 200 }
hsh.each{|key, value| puts "#{key} is #{value}" }
# produces:
a is 100
b is 200

with block arity:

hsh = {[:a,:b] => 3, [:c, :d] => 4, :e => 5}
seen_args = []
hsh.each{|arg1, arg2, arg3| seen_args << [arg1, arg2, arg3] }
# => [[[:a, :b], 3, nil], [[:c, :d], 4, nil], [:e, 5, nil]]

seen_args = []
hsh.each{|(arg1, arg2), arg3| seen_args << [arg1, arg2, arg3] }
# => [[:a, :b, 3], [:c, :d, 4], [:e, nil, 5]]

Overloads:

  • #hsh.each {|key, val| ... } ⇒ Hashlike

    Calls block once for each key in +hsh+

    Yields:

    • (key, val)

      in order, each key and its associated value

    Returns:

  • #hsh.each(->an_enumerator) ⇒ Enumerator

    with no block, returns a raw enumerator

    Returns:

    • (Enumerator)


153
154
155
156
# File 'lib/gorillib/hashlike.rb', line 153

def each(&block)
  return enum_for(:each) unless block_given?
  each_pair(&block)
end

#hsh.each_pair {|key, val| ... } ⇒ Hashlike #hsh.each_pair(->an_enumerator) ⇒ Enumerator

Calls +block+ once for each key in +hsh+, passing the key/value pair as parameters.

If no block is given, an enumerator is returned instead.

Examples:

hsh = { :a => 100, :b => 200 }
hsh.each_pair{|key, value| puts "#{key} is #{value}" }
# produces:
a is 100
b is 200

with block arity:

hsh = {[:a,:b] => 3, [:c, :d] => 4, :e => 5}
seen_args = []
hsh.each_pair{|arg1, arg2, arg3| seen_args << [arg1, arg2, arg3] }
# => [[[:a, :b], 3, nil], [[:c, :d], 4, nil], [:e, 5, nil]]

seen_args = []
hsh.each_pair{|(arg1, arg2), arg3| seen_args << [arg1, arg2, arg3] }
# => [[:a, :b, 3], [:c, :d, 4], [:e, nil, 5]]

Overloads:

  • #hsh.each_pair {|key, val| ... } ⇒ Hashlike

    Calls block once for each key in +hsh+

    Yields:

    • (key, val)

      in order, each key and its associated value

    Returns:

  • #hsh.each_pair(->an_enumerator) ⇒ Enumerator

    with no block, returns a raw enumerator

    Returns:

    • (Enumerator)


113
114
115
116
117
118
119
# File 'lib/gorillib/hashlike.rb', line 113

def each_pair
  return enum_for(:each_pair) unless block_given?
  keys.each do |key|
    yield([key, self[key]])
  end
  self
end

#lengthFixnum

Returns the number of key/value pairs in the hashlike.

Examples:

hsh = { :d => 100, :a => 200, :v => 300, :e => 400 }
hsh.length       # => 4
hsh.delete(:a)   # => 200
hsh.length       # => 3

Returns:

  • (Fixnum)

    number of key-value pairs



169
170
171
# File 'lib/gorillib/hashlike.rb', line 169

def length
  keys.length
end

#valuesArray

A new array populated with the values from +hsh+.

Examples:

hsh = { :a => 100, :b => 200, :c => 300 }
hsh.values   # => [100, 200, 300]

Returns:

  • (Array)

    the values, in order by their key.

See Also:

  • Hashlike#keys.


184
185
186
# File 'lib/gorillib/hashlike.rb', line 184

def values
  [].tap{|arr| each_pair{|key, val| arr << val } }
end

#values_at(*allowed_keys) ⇒ Array

Array containing the values associated with the given keys.

Examples:

hsh = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
hsh.values_at("cow", "cat")  # => ["bovine", "feline"]
hsh = { :a => 100, :b => 200, :c => 300 }
hsh.values_at(:c, :a, :c, :z, :a)
# => [300, 100, 300, nil, 100]

Parameters:

  • allowed_keys (Object)

    the keys to retrieve.

Returns:

  • (Array)

    the values, in order according to allowed_keys.

See Also:

  • Hashlike#select.


205
206
207
208
209
210
# File 'lib/gorillib/hashlike.rb', line 205

def values_at(*allowed_keys)
  allowed_keys.map do |key|
    key = convert_key(key) if respond_to?(:convert_key)
    self[key] if has_key?(key)
  end
end