Module: Enumerable

Defined in:
lib/invoca/utils/map_compact.rb,
lib/invoca/utils/stable_sort.rb

Instance Method Summary collapse

Instance Method Details

#map_compactObject



2
3
4
5
6
7
8
9
# File 'lib/invoca/utils/map_compact.rb', line 2

def map_compact
  result = []
  each do |item|
    selected = yield(item)
    result << selected unless selected.nil?
  end
  result
end

#stable_sortObject

A stable sort will preserve the original order of two elements if their sort keys are identical.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/invoca/utils/stable_sort.rb', line 5

def stable_sort
  n = -1
  if block_given?
    collect {|x| n += 1; [x, n]
    }.sort! {|a, b|
      c = yield a[0], b[0]
      if c.nonzero? then c else a[1] <=> b[1] end
    }.collect! {|x| x[0]}
  else
    sort_by {|x| n += 1; [x, n]}
  end
end

#stable_sort_byObject



18
19
20
21
22
# File 'lib/invoca/utils/stable_sort.rb', line 18

def stable_sort_by
  block_given? or return enum_for(:stable_sort_by)
  n = -1
  sort_by {|x| n += 1; [(yield x), n]}
end