Module: Enumerable

Defined in:
lib/mug/top.rb,
lib/mug/bool.rb,
lib/mug/to_h.rb,
lib/mug/counts.rb

Instance Method Summary collapse

Instance Method Details

#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

#to_hObject

Converts enum to a Hash.

Each element of enum must be a single item, or an array of two items. Duplicate keys are overwritten in order.

[].to_h             #=> {}
[1,2].to_h          #=> {1=>nil, 2=>nil}
(1..2).to_h         #=> {1=>nil, 2=>nil}
[[1,2],[3,4]].to_h  #=> {1=>2, 3=>4}
[[1,2],[1,4]].to_h  #=> {1=>4}


23
24
25
26
27
28
29
30
# File 'lib/mug/to_h.rb', line 23

def to_h
	hsh = {}
	each do |k,v,*x|
		raise ArgumentError, "invalid number of elements (#{x.length+2} for 1..2)" if x.any?
		hsh[k] = v
	end
	hsh
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