module Enumerable
Public Class Methods
Invokes a block once for every element in a sequence of Enumerables.
# File lib/mug/enumerable/chain.rb, line 9 def chain *enums return enum_for(:chain, *enums) unless block_given? enums.each do |enum| enum.each {|*args| yield(*args) } end end
Public Instance Methods
Passes each element of the collection to the given block. The method returns `true` if the block contains elements that never return `false` or `nil`. If the block is not given, Ruby adds an implicit block of `{ |obj| obj }` which will cause `any_and_all?` to return `true` when none of the collection members are `false` or `nil`.
# File lib/mug/enumerable/any-and-all.rb, line 10 def any_and_all? &block block ||= proc {|obj| obj } result = false each do |x| return false unless block[x] result = true end result end
Get the bottom n
items, ordered from bottom to top.
Returns an Array
even when n
is 1.
@see Enumerable#sort
# File lib/mug/top.rb, line 58 def bottom n=1, &blk if block_given? sort(&blk)[0...n] else #bottom_by(n) {|x| x } sort[0...n] end end
Get the bottom n
items, in order from bottom to top, ordered by mapping the values through the given block.
Returns an Array
even when n
is 1. Values that are tied after mapping are returned in the initial order.
If no block is given, an enumerator is returned instead.
@see Enumerable#sort_by
# File lib/mug/top.rb, line 78 def bottom_by n=1, &_blk return enum_for(:bottom_by, n) unless block_given? chain = {} each do |x| y = yield x chain[y] ||= [] chain[y] << x end ary = [] chain.keys.sort.each do |k| ary += chain[k] break if ary.length > n end ary[0...n] end
Creates a chain of Enumerables following this one, and invokes a block once for each element of each Enumerable
.
# File lib/mug/enumerable/chain.rb, line 26 def chain *enums return enum_for(:chain, *enums) unless block_given? [self, *enums].each do |enum| enum.each {|*args| yield(*args) } end nil end
Returns a hash of item=>count showing how many of each item
are in this Enumerable
.
# File lib/mug/enumerable/counts.rb, line 8 def counts &block return counts_by(&block) if block_given? hsh = Hash.new{|h,k| h[k] = 0 } each do |k| hsh[k] += 1 end hsh end
Passes each element in turn to the block, and returns a hash of result=>count.
If no block is given, an enumerator is returned.
# File lib/mug/enumerable/counts.rb, line 23 def counts_by &_block return enum_for(:counts_by) unless block_given? hsh = Hash.new{|h,k| h[k] = 0 } each do |j| k = yield j hsh[k] += 1 end hsh end
Calls block
once for each key in the enum, passing the key as a parameter. If no block is given, an enumerator is returned instead.
@call-seq each_key
{| key | block } -> hsh @call-seq each_key
-> an_enumerator
# File lib/mug/enumerable/hash-like.rb, line 23 def each_key return enum_for(:each_key) unless block_given? # FIXME each_with_index {|_,i| yield i } end
Calls block
once for each key in the enum, passing the key-value pair as parameters. If no block is given, an enumerator is returned instead.
@call-seq each_pair
{| key, value | block } -> hsh @call-seq each_pair
-> an_enumerator
# File lib/mug/enumerable/hash-like.rb, line 11 def each_pair return enum_for(:each_pair) unless block_given? each_with_index {|e,i| yield i, e } end
Returns a value from the enum for the given key. If the key can't be found, there are several options: With no other arguments, it will raise a KeyError
exception; if default
is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
@call-seq fetch(key [, default] ) -> obj @call-seq fetch(key) { |key| block } -> obj
# File lib/mug/enumerable/hash-like.rb, line 42 def fetch *args raise ArgumentError, "incorrect number of arguments (#{args.length} for 1..2)" if args.length < 1 || args.length > 2 key, default = *args each_pair do |k,v| return v if k == key end if args.length > 1 default elsif block_given? yield key else raise KeyError, 'key not found' end end
Returns an array containing the values associated with the given keys but also raises KeyError
when one of keys can't be found. Also see #values_at
and #fetch
.
@call-seq fetch_values
(key, …) -> array @call-seq fetch_values
(key, …) { |key| block } -> array
# File lib/mug/enumerable/hash-like.rb, line 66 def fetch_values *keys key_map = {} keys.each_with_index do |key, index| key_map[key] ||= [] key_map[key] << index end remain = keys.length result = [nil] * keys.length define = [nil] * keys.length each_pair do |k,v| break if remain < 1 key_indices = key_map[k] next unless key_indices key_indices.each do |key_index| result[key_index] = v define[key_index] = true remain -= 1 end end if remain > 0 if block_given? define.each_with_index do |isdef, index| next if isdef result[index] = yield keys[index] end else index = define.index nil raise KeyError, "key not found: #{keys[index].inspect}" end end result end
Returns true
if the given key is present in this enum.
@call-seq key?(key) -> true or false
# File lib/mug/enumerable/hash-like.rb, line 112 def key? key each_pair.find {|k, _| key == k } && true end
Returns a new array populated with the keys from this enum.
@call-seq keys -> array
# File lib/mug/enumerable/hash-like.rb, line 122 def keys each_with_index.map {|_, index| index } end
Returns the number of key-value pairs in the hash.
@call-seq length -> integer
# File lib/mug/enumerable/hash-like.rb, line 131 def length reduce(0) {|sum,_| sum + 1 } end
Returns true if the given key is present in this enum.
@call-seq member?(key) -> true or false
# File lib/mug/enumerable/hash-like.rb, line 142 def member? key fetch(key, nil) && true end
Returns a hash containing only the given keys and their values.
@call-seq slice(*keys) -> a_hash
# File lib/mug/enumerable/hash-like.rb, line 151 def slice *keys missing = {} values = fetch_values(*keys) {|key| missing[key] = nil } result = {} values.each_with_index do |val, i| key = keys[i] next if missing.key? key next if result.key? key result[key] = val end result end
Converts enum to a boolean. Returns true if there are any elements.
# File lib/mug/bool.rb, line 86 def to_b any?{ true } end
Get the top n
items, in order from top to bottom.
Returns an Array
even when n
is 1.
@see Enumerable#sort
# File lib/mug/top.rb, line 11 def top n=1, &_blk if block_given? sort{|x,y| yield y, x }[0...n] else #top_by(n) {|x| x } if n <= length sort[-n..-1].reverse else sort.reverse end end end
Get the top n
items, in order from top to bottom, ordered by mapping the values through the given block.
Returns an Array
even when n
is 1. Values that are tied after mapping are returned in the initial order.
If no block is given, an enumerator is returned instead.
@see Enumerable#sort_by
# File lib/mug/top.rb, line 35 def top_by n=1, &_blk return enum_for(:top_by, n) unless block_given? chain = {} each do |x| y = yield x chain[y] ||= [] chain[y] << x end ary = [] chain.keys.sort.reverse.each do |k| ary += chain[k] break if ary.length > n end ary[0...n] end
Returns a new hash with the results of running the block once for every key. If no block is given, an enumerator is returned instead.
@call-seq transform_keys
{|key| block } -> new_hash @call-seq transform_keys
-> an_enumerator
# File lib/mug/enumerable/hash-like.rb, line 172 def transform_keys return enum_for(:transform_keys) unless block_given? result = {} each_pair do |key, value| newkey = yield key result[newkey] = value end result end
Returns a new hash with the results of running the block once for every value. If no block is given, an enumerator is returned instead.
@call-seq transform_values
{|value| block } -> new_hash @call-seq transform_values
-> an_enumerator
# File lib/mug/enumerable/hash-like.rb, line 189 def transform_values return enum_for(:transform_values) unless block_given? result = {} each_pair do |key, value| newvalue = yield value result[key] = newvalue end result end
Returns true
if the given value is present for some key.
@call-seq value?(value) -> true or false
# File lib/mug/enumerable/hash-like.rb, line 204 def value? value include? value end
Returns a new array populated with the values from this enum.
@call-seq values -> array
# File lib/mug/enumerable/hash-like.rb, line 214 def values to_a end
Return an array containing the values associated with the given keys.
@call-seq values_at
(key, …) -> array
# File lib/mug/enumerable/hash-like.rb, line 223 def values_at *keys fetch_values(*keys) {|key| nil } end