Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Easily add rankings to arrays of objects.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 License.txt
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 rank.gemspec
README.markdown

Rank

Easily add rankings to arrays of objects.

Installation

gem install rank

Getting Started

Add rankings to your objects:

things = [{:attr => 'a'}, {:attr => 'c'}, {:attr => 'b'}]
Rank.add things, :attr
=> [{:attr=>"a", :rank=>1}, {:attr=>"b", :rank=>2}, {:attr=>"c", :rank=>3}]

You can add rank to ActiveRecord objects as well:

Rank.add User.all, :firstname, :lastname

In general anything that responds to '[]' can have a rank added to it.

Additional Options

Adding a sorting order:

Rank.add User.all, [:firstname, :asc], [:lastname, :desc]

You can specify a conversion to floating point or integer:

Rank.add User.all, [:id, :asc, :integer]

You can choose to honor ties or to ignore them:

things = [{:attr => 'a'}, {:attr => 'c'}, {:attr => 'b'}, {:attr => 'b'}]

Rank.add things, :attr, :ties => false
=> [{:attr=>"a", :rank=>1}, {:attr=>"b", :rank=>2}, {:attr=>"b", :rank=>3}, {:attr=>"c", :rank=>4}]

Rank.add things, :attr, :ties => true
=> [{:attr=>"a", :rank=>1}, {:attr=>"b", :rank=>2}, {:attr=>"b", :rank=>2}, {:attr=>"c", :rank=>4}]

You can leave the objects in their original positions:

things = [{:attr => 'a'}, {:attr => 'c'}, {:attr => 'b'}]
Rank.add things, :attr, :sort => false
[{:attr=>"a", :rank=>1}, {:attr=>"c", :rank=>2}, {:attr=>"b", :rank=>2}]
Something went wrong with that request. Please try again.