diff --git a/lib/competition_ranking_leaderboard.rb b/lib/competition_ranking_leaderboard.rb index 95f7a96..00347a5 100644 --- a/lib/competition_ranking_leaderboard.rb +++ b/lib/competition_ranking_leaderboard.rb @@ -9,6 +9,8 @@ class CompetitionRankingLeaderboard < Leaderboard # @return the rank for a member in the leaderboard. def rank_for_in(leaderboard_name, member) member_score = score_for_in(leaderboard_name, member) + return nil if member_score.nil? + if @reverse return @redis_connection.zcount(leaderboard_name, '-inf', "(#{member_score}") + 1 rescue nil else diff --git a/spec/competition_ranking_leaderboard_spec.rb b/spec/competition_ranking_leaderboard_spec.rb index bcb9b2f..866e38d 100644 --- a/spec/competition_ranking_leaderboard_spec.rb +++ b/spec/competition_ranking_leaderboard_spec.rb @@ -184,4 +184,11 @@ expect(leaders[2][:member]).to eql('member_25') end end + + context 'ranks' do + it 'should return nil rank for unranked member' do + leaderboard = CompetitionRankingLeaderboard.new('ranks', Leaderboard::DEFAULT_OPTIONS, {:host => "127.0.0.1", :db => 15}) + expect(leaderboard.rank_for('unranked_member')).to eq(nil) + end + end end