Method: Redis::Commands::SortedSets#zrange

Defined in:
lib/redis/commands/sorted_sets.rb

#zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex, rev: false, limit: nil, withscores: false, with_scores: withscores) ⇒ Array<String>, Array<[String, Float]>

Return a range of members in a sorted set, by index, score or lexicographical ordering.

Examples:

Retrieve all members from a sorted set, by index

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 (Integer)

    start index

  • stop (Integer)

    stop index

  • options (Hash)
    • ‘:by_score => false`: return members by score

    • ‘:by_lex => false`: return members by lexicographical ordering

    • ‘:rev => false`: reverse the ordering, from highest to lowest

    • ‘:limit => [offset, count]`: skip `offset` members, return a maximum of

    ‘count` members

    • ‘: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



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/redis/commands/sorted_sets.rb', line 301

def zrange(key, start, stop, byscore: false, by_score: byscore, bylex: false, by_lex: bylex,
           rev: false, limit: nil, withscores: false, with_scores: withscores)

  if by_score && by_lex
    raise ArgumentError, "only one of :by_score or :by_lex can be specified"
  end

  args = [:zrange, key, start, stop]

  if by_score
    args << "BYSCORE"
  elsif by_lex
    args << "BYLEX"
  end

  args << "REV" if rev

  if limit
    args << "LIMIT"
    args.concat(limit.map { |l| Integer(l) })
  end

  if with_scores
    args << "WITHSCORES"
    block = FloatifyPairs
  end

  send_command(args, &block)
end