Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/rmmseg/rule_helper.rb

Instance Method Summary collapse

Instance Method Details

#take_highestObject

Take the elements with the highest value. Value are compared through the block. e.g

["aaaa", "bb", "cccc"].take_highest { |a, b|
  a.length <=> b.length
}
# => ["aaaa", "cccc"]


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rmmseg/rule_helper.rb', line 10

def take_highest
  return [] if empty?
  
  rlt = [self.first]
  max = self.first

  for i in 1...length
    cmp = yield(self[i], max)
    if cmp == 0
      rlt << self[i]
    elsif cmp > 0
      max = self[i]
      rlt = [max]
    end
  end

  rlt
end