Ranker
A Ruby library for ranking scorable types using various ranking strategies.
Compatibility
Ranker is tested against MRI (1.8.7+) and JRuby (1.9.0+).
Installation
With bundler, add the ranker
gem to your Gemfile
.
gem "ranker", "~> 1.0"
Require the ranker
gem in your application.
require "ranker"
Usage
Default Ranking
Default ranking will assume values are numeric and rank them in descending order.
scores = [1, 1, 2, 3, 3, 1, 4, 4, 5, 6, 8, 1, 0]
rankings = Ranker.rank(scores)
rankings.count #=> 8
ranking_1 = rankings[0]
ranking_1.rank #=> 1
ranking_1.score #=> 8
Custom Ranking
Custom ranking allows for ranking of objects by using a symbol or a lambda.
class Player
attr_accesor :score
def initalize(score)
@score = score
end
end
players = [Player.new(0), Player.new(100), Player.new(1000), Player.new(25)]
rankings = Ranker.rank(players, :by => lambda { |player| player.score })
# or
rankings = Ranker.rank(players, :by => :score)
In some cases objects need to be ranked by score in ascending order, for example, if you were ranking golf players.
class GolfPlayer < Player
end
players = [GolfPlayer.new(72), GolfPlayer.new(100), GolfPlayer.new(138), GolfPlayer.new(54)]
rankings = Ranker.rank(players, :by => :score, :desc => false)
Ranking Strategies
Ranker has a number of ranking strategies available to use, mostly based on the Wikipedia entry on ranking. Strategies can be passed in as an option to the rank method.
rankings = Ranker.rank(players, :by => :score, :strategy => :ordinal)
Standard Competition Ranking ("1224" ranking)
This is the default ranking strategy. For more info, see the Wikipedia entry on Standard Competition Ranking.
rankings = Ranker.rank(players, :by => :score, :strategy => :standard_competition)
Modified Competition Ranking ("1334" ranking)
For more info, see the Wikipedia entry on Modified Competition Ranking.
rankings = Ranker.rank(players, :by => :score, :strategy => :modified_competition)
Dense Ranking ("1223" ranking)
For more info, see the Wikipedia entry on Dense Ranking.
rankings = Ranker.rank(players, :by => :score, :strategy => :dense)
Ordinal Ranking ("1234" ranking)
For more info, see the Wikipedia entry on Ordinal Ranking.
rankings = Ranker.rank(players, :by => :score, :strategy => :ordinal)
Custom Ranking
If you find the current strategies not to your liking, you can write your own and pass the class into the rank method.
class MyCustomStrategy < Ranker::Strategies::Strategy
def execute
# My code here
end
end
rankings = Ranker.rank(players, :by => :score, :strategy => MyCustomStrategy)
Copyright
Copyright © 2013 Ilya Scharrenbroich. Released under the MIT License.