Sha256: 1823e1ec089f5ada65d51e0fe7eb9728f9e32ce758de6ec9b5a2df74fd664bd8

Contents?: true

Size: 627 Bytes

Versions: 1

Compression:

Stored size: 627 Bytes

Contents

require 'forwardable'

class Ranking
  extend Forwardable

  def initialize
    @items_to_score = {}
  end

  def top(num=nil)
    if(num.is_a?(Numeric))
      sorted_items[0,num]
    else
      sorted_items
    end
  end

  def percentile(item)
    index = sorted_items.index(item)
    worse_item_count = (length - (index+1))
    worse_item_count.to_f/length
  end

  def_delegator :@items_to_score, :has_key?, :scored?
  def_delegators :@items_to_score, :[], :[]=, :length, :each, :delete

  private

  def sorted_items
    @sorted_items ||= @items_to_score.sort_by {|item, score| -score}.map {|item, score| item}
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metric_fu-2.0.0 lib/base/ranking.rb