Method: Redis#zrange

Defined in:
lib/redis.rb

#zrange(key, start, stop, options = {}) ⇒ Array<String>, Array<[String, Float]>

Return a range of members in a sorted set, by index.

Examples:

Retrieve all members from a sorted set

redis.zrange("zset", 0, -1)
  # => ["a", "b"]

Retrieve all members and their scores from a sorted set

redis.zrange("zset", 0, -1, :with_scores => true)
  # => [["a", 32.0], ["b", 64.0]]

Parameters:

  • key (String)
  • start (Fixnum)

    start index

  • stop (Fixnum)

    stop index

  • options (Hash) (defaults to: {})
    • :with_scores => true: include scores in output

Returns:

  • (Array<String>, Array<[String, Float]>)
    • when :with_scores is not specified, an array of members
    • when :with_scores is specified, an array with [member, score] pairs


921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/redis.rb', line 921

def zrange(key, start, stop, options = {})
  args = []

  with_scores = options[:with_scores] || options[:withscores]
  args << "WITHSCORES" if with_scores

  synchronize do
    @client.call [:zrange, key, start, stop, *args] do |reply|
      if with_scores
        if reply
          reply.each_slice(2).map do |member, score|
            [member, Float(score)]
          end
        end
      else
        reply
      end
    end
  end
end