Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Easily add rankings to arrays of objects.
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
Gemfile
License.txt
README.markdown
Rakefile
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.