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



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

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.



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

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



20
21
22
23
24
# File 'lib/invoca/utils/stable_sort.rb', line 20

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