Method: Redis::Commands::SortedSets#zmpop

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

#zmpop(*keys, modifier: "MIN", count: nil) ⇒ Array<String, Array<String, Float>>

Removes and returns up to count members with scores in the sorted set stored at key.

Examples:

Popping a member

redis.zmpop('zset')
#=> ['zset', ['a', 1.0]]

With count option

redis.zmpop('zset', count: 2)
#=> ['zset', [['a', 1.0], ['b', 2.0]]

Returns:

  • (Array<String, Array<String, Float>>)

    list of popped elements and scores

Raises:

  • (ArgumentError)


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/redis/commands/sorted_sets.rb', line 220

def zmpop(*keys, modifier: "MIN", count: nil)
  raise ArgumentError, "Pick either MIN or MAX" unless modifier == "MIN" || modifier == "MAX"

  args = [:zmpop, keys.size, *keys, modifier]
  args << "COUNT" << Integer(count) if count

  send_command(args) do |response|
    response&.map do |entry|
      case entry
      when String then entry
      when Array then entry.map { |pair| FloatifyPairs.call(pair) }.flatten(1)
      end
    end
  end
end