Module: Enumerable

Defined in:
lib/mug/top.rb,
lib/mug/bool.rb,
lib/mug/counts.rb,
lib/mug/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 and_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/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/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/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

#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