You would use the option, :reverse => true, if you wanted a leaderboard sorted from lowest-to-highest score. You may also set the reverse option on a leaderboard after you have created a new instance of a leaderboard. The various ..._key options above control what data is returned in the hash of leaderboard data from calls such as leaders or around_me. Finally, the global_member_data option allows you to control whether optional member data is per-leaderboard (false) or global (true).

If you need to pass in options for Redis, you can do this in the initializer:

Ranking members in the leaderboard

You can call rank_member with the same member and the leaderboard will be updated automatically.

Get some information about your leaderboard:

highscore_lb.total_members
=> 10
highscore_lb.total_pages
=> 1

The rank_member call will also accept an optional parameter, member_data that could
be used to store other information about a given member in the leaderboard. This
may be useful in situations where you are storing member IDs in the leaderboard and
you want to be able to store a member name for display. You could use JSON to
encode a Hash of member data. Example:

If you delete the leaderboard, ALL of the member data is deleted as well.

Optional member data notes

If you use optional member data, the use of the remove_members_in_score_range or remove_members_outside_rank methods
will leave data around in the member data hash. This is because the internal Redis method, zremrangebyscore,
only returns the number of items removed. It does not return the members that it removed.

:include_missing - true (default) or false to return members that are not ranked.

You can also use the members and members_in methods as aliases for the leaders and leaders_in methods.

There are also a few convenience methods to be able to retrieve all leaders from a given leaderboard. They are all_leaders and all_leaders_from. You may also use the aliases all_members or all_members_from. Use any of these methods sparingly as all the information in the leaderboard will be returned.

Ranking a member across multiple leaderboards

Alternate leaderboard types

The leaderboard library offers 3 styles of ranking. This is only an issue for members with the same score in a leaderboard.

Default: The Leaderboard class uses the default Redis sorted set ordering, whereby different members having the same score are ordered lexicographically. As per the Redis documentation on Redis sorted sets, "The lexicographic ordering used is binary, it compares strings as array of bytes."

Tie ranking: The TieRankingLeaderboard subclass of Leaderboard allows you to define a leaderboard where members with the same score are given the same rank. For example, members in a leaderboard with the associated scores would have the ranks of:

The TieRankingLeaderboard accepts one additional option, :ties_namespace (default: ties), when initializing a new instance of this class. Please note that in its current implementation, the TieRankingLeaderboard class uses an additional sorted set to rank the scores, so please keep this in mind when you are doing any capacity planning for Redis with respect to memory usage.

Competition ranking: The CompetitionRankingLeaderboard subclass of Leaderboard allows you to define a leaderboard where members with the same score will have the same rank, and then a gap is left in the ranking numbers. For example, members in a leaderboard with the associated scores would have the ranks of:

Other useful methods

delete_leaderboard: Delete the current leaderboard
member_data_for(member): Retrieve the optional member data for a given member in the leaderboard
members_data_for(members): Retrieve the optional member data for a given list of members in the leaderboard
update_member_data(member, member_data): Update the optional member data for a given member in the leaderboard
remove_member_data(member): Remove the optional member data for a given member in the leaderboard
remove_member(member): Remove a member from the leaderboard
total_members: Total # of members in the leaderboard
total_pages: Total # of pages in the leaderboard given the leaderboard's page_size
total_members_in_score_range(min_score, max_score): Count the number of members within a score range in the leaderboard
change_score_for(member, delta): Change the score for a member by some amount delta (delta could be positive or negative)
rank_for(member): Retrieve the rank for a given member in the leaderboard
score_for(member): Retrieve the score for a given member in the leaderboard
check_member?(member): Check to see whether member is in the leaderboard
score_and_rank_for(member): Retrieve the score and rank for a member in a single call
remove_members_in_score_range(min_score, max_score): Remove members from the leaderboard within a score range
remove_members_outside_rank(rank): Remove members from the leaderboard outside a given rank
percentile_for(member): Calculate the percentile for a given member
score_for_percentile(percentile): Calculate the score for a given percentile value in the leaderboard
page_for(member, page_size): Determine the page where a member falls in the leaderboard
expire_leaderboard(seconds): Expire the leaderboard in a set number of seconds.
expire_leaderboard_at(timestamp): Expire the leaderboard at a specific UNIX timestamp.
rank_members(members_and_scores): Rank an array of members in the leaderboard where you can call via (member_name, score) or pass in an array of [member_name, score]
merge_leaderboards(destination, keys, options = {:aggregate => :min}): Merge leaderboards given by keys with this leaderboard into destination
intersect_leaderboards(destination, keys, options = {:aggregate => :min}): Intersect leaderboards given by keys with this leaderboard into destination
top(number, options): Retrieve members from the leaderboard within a range from 1 to the number given.

Contributing to leaderboard

Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

Fork the project

Start a feature/bugfix branch

Commit and push until you are happy with your contribution

Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2011-2016 David Czarnecki. See LICENSE.txt for further details.