Module: Enumerable

Defined in:
lib/mug/top.rb,
lib/mug/bool.rb,
lib/mug/enumerable/counts.rb,
lib/mug/enumerable/hash-like.rb,
lib/mug/enumerable/any-and-all.rb

Instance Method Summary collapse

Instance Method Details

#any_and_all?(&block) ⇒ Boolean

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`.

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# 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

#bottom(n = 1, &blk) ⇒ Object

Get the bottom n items, ordered from bottom to top.

Returns an Array even when n is 1.

See Also:

  • #sort


58
59
60
61
62
63
64
65
# 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

#bottom_by(n = 1, &blk) ⇒ Object

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 Also:

  • #sort_by


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#counts(&block) ⇒ Object

Returns a hash of item=>count showing how many of each item are in this Enumerable.



8
9
10
11
12
13
14
15
# 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

#counts_by(&block) ⇒ Object

Passes each element in turn to the block, and returns a hash of result=>count.

If no block is given, an enumerator is returned.



23
24
25
26
27
28
29
30
31
# 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

#each_keyObject

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



23
24
25
26
27
28
# 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

#each_pairObject

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



11
12
13
14
# 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

#fetch(*args) ⇒ Object

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

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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
    return default
  elsif block_given?
    yield key
  else
    raise KeyError, 'key not found'
  end
end

#fetch_values(*keys) ⇒ Object

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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]
    if key_indices
      key_indices.each do |key_index|
        result[key_index] = v
        define[key_index] = true
        remain -= 1
      end
    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

#key?(key) ⇒ Boolean Also known as: has_key?

Returns true if the given key is present in this enum.

@call-seq key?(key) -> true or false

Returns:

  • (Boolean)


112
113
114
# File 'lib/mug/enumerable/hash-like.rb', line 112

def key? key
  each_pair.find {|k, _| key == k } && true
end

#keysObject

Returns a new array populated with the keys from this enum.

@call-seq keys -> array



122
123
124
# File 'lib/mug/enumerable/hash-like.rb', line 122

def keys
  each_with_index.map {|_, index| index }
end

#lengthObject Also known as: size

Returns the number of key-value pairs in the hash.

@call-seq length -> integer



131
132
133
# File 'lib/mug/enumerable/hash-like.rb', line 131

def length
  reduce(0) {|sum,_| sum + 1 }
end

#member?(key) ⇒ Boolean

Returns true if the given key is present in this enum.

@call-seq member?(key) -> true or false

Returns:

  • (Boolean)


141
142
143
# File 'lib/mug/enumerable/hash-like.rb', line 141

def member? key
  fetch(key, nil) && true
end

#slice(*keys) ⇒ Object

Returns a hash containing only the given keys and their values.

@call-seq slice(*keys) -> a_hash



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mug/enumerable/hash-like.rb', line 150

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

#to_bObject

Converts enum to a boolean. Returns true if there are any elements.



86
87
88
# File 'lib/mug/bool.rb', line 86

def to_b
  any?{ true }
end

#top(n = 1, &blk) ⇒ Object

Get the top n items, in order from top to bottom.

Returns an Array even when n is 1.

See Also:

  • #sort


11
12
13
14
15
16
17
18
19
20
21
22
# 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

#top_by(n = 1, &blk) ⇒ Object

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 Also:

  • #sort_by


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

#transform_keysObject

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



171
172
173
174
175
176
177
178
179
# File 'lib/mug/enumerable/hash-like.rb', line 171

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

#transform_valuesObject

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



188
189
190
191
192
193
194
195
196
# File 'lib/mug/enumerable/hash-like.rb', line 188

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

#value?(value) ⇒ Boolean Also known as: has_value?

Returns true if the given value is present for some key.

@call-seq value?(value) -> true or false

Returns:

  • (Boolean)


203
204
205
# File 'lib/mug/enumerable/hash-like.rb', line 203

def value? value
  include? value
end

#valuesObject

Returns a new array populated with the values from this enum.

@call-seq values -> array



213
214
215
# File 'lib/mug/enumerable/hash-like.rb', line 213

def values
  to_a
end

#values_at(*keys) ⇒ Object

Return an array containing the values associated with the given keys.

@call-seq values_at(key, …) -> array



222
223
224
# File 'lib/mug/enumerable/hash-like.rb', line 222

def values_at *keys
  fetch_values(*keys) {|key| nil }
end