module Gorillib::Hashlike::EnumerateFromKeys

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.

Public Instance Methods

each(&block) click to toggle source

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.

@example

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

@example 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]]

@overload hsh.each{|key, val| block } -> hsh

Calls block once for each key in +hsh+
@yield [key, val] in order, each key and its associated value
@return [Hashlike]

@overload hsh.each -> an_enumerator

with no block, returns a raw enumerator
@return [Enumerator]
# File lib/gorillib/hashlike.rb, line 153
def each(&block)
  return enum_for(:each) unless block_given?
  each_pair(&block)
end
each_pair() { |key, self| ... } click to toggle source

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.

@example

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

@example 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]]

@overload hsh.each_pair{|key, val| block } -> hsh

Calls block once for each key in +hsh+
@yield [key, val] in order, each key and its associated value
@return [Hashlike]

@overload hsh.each_pair -> an_enumerator

with no block, returns a raw enumerator
@return [Enumerator]
# 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
length() click to toggle source

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

@example

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

@return [Fixnum] number of key-value pairs

# File lib/gorillib/hashlike.rb, line 169
def length
  keys.length
end
values() click to toggle source

A new array populated with the values from hsh.

@see Hashlike#keys.

@example

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

@return [Array] the values, in order by their key.

# File lib/gorillib/hashlike.rb, line 184
def values
  [].tap{|arr| each_pair{|key, val| arr << val } }
end
values_at(*allowed_keys) click to toggle source

Array containing the values associated with the given keys.

@see Hashlike#select.

@example

hsh = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
hsh.values_at("cow", "cat")  # => ["bovine", "feline"]

@example

hsh = { :a => 100, :b => 200, :c => 300 }
hsh.values_at(:c, :a, :c, :z, :a)
# => [300, 100, 300, nil, 100]

@param allowed_keys [Object] the keys to retrieve. @return [Array] the values, in order according to allowed_keys.

# 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