class Array

Public Instance Methods

take_highest() { |self, max| ... } click to toggle source

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"]
# 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