module Redis::Commands::Hashes

def hdel(key, *fields)

Returns:
  • (Integer) - the number of fields that were removed from the hash

Parameters:
  • field (String, Array) --
  • key (String) --
def hdel(key, *fields)
  fields.flatten!(1)
  send_command([:hdel, key].concat(fields))
end

def hexists(key, field)

Returns:
  • (Boolean) - whether or not the field exists in the hash

Parameters:
  • field (String) --
  • key (String) --
def hexists(key, field)
  send_command([:hexists, key, field], &Boolify)
end

def hget(key, field)

Returns:
  • (String) -

Parameters:
  • field (String) --
  • key (String) --
def hget(key, field)
  send_command([:hget, key, field])
end

def hgetall(key)

Returns:
  • (Hash) -

Parameters:
  • key (String) --
def hgetall(key)
  send_command([:hgetall, key], &Hashify)
end

def hincrby(key, field, increment)

Returns:
  • (Integer) - value of the field after incrementing it

Parameters:
  • increment (Integer) --
  • field (String) --
  • key (String) --
def hincrby(key, field, increment)
  send_command([:hincrby, key, field, Integer(increment)])
end

def hincrbyfloat(key, field, increment)

Returns:
  • (Float) - value of the field after incrementing it

Parameters:
  • increment (Float) --
  • field (String) --
  • key (String) --
def hincrbyfloat(key, field, increment)
  send_command([:hincrbyfloat, key, field, Float(increment)], &Floatify)
end

def hkeys(key)

Returns:
  • (Array) -

Parameters:
  • key (String) --
def hkeys(key)
  send_command([:hkeys, key])
end

def hlen(key)

Returns:
  • (Integer) - number of fields in the hash

Parameters:
  • key (String) --
def hlen(key)
  send_command([:hlen, key])
end

def hmget(key, *fields, &blk)

Other tags:
    See: #mapped_hmget -

Returns:
  • (Array) - an array of values for the specified fields

Parameters:
  • fields (Array) -- array of fields
  • key (String) --
def hmget(key, *fields, &blk)
  fields.flatten!(1)
  send_command([:hmget, key].concat(fields), &blk)
end

def hmset(key, *attrs)

Other tags:
    See: #mapped_hmset -

Returns:
  • (String) - `"OK"`

Parameters:
  • attrs (Array) -- array of fields and values
  • key (String) --
def hmset(key, *attrs)
  send_command([:hmset, key] + attrs)
end

def hrandfield(key, count = nil, withvalues: false, with_values: withvalues)

Returns:
  • (nil, String, Array, Array<[String, Float]>) -

Parameters:
  • options (Hash) --
  • count (Integer) --
  • key (String) --

Other tags:
    Example: Get multiple random fields with values -
    Example: Get multiple random fields -
    Example: Get one random field -
def hrandfield(key, count = nil, withvalues: false, with_values: withvalues)
  if with_values && count.nil?
    raise ArgumentError, "count argument must be specified"
  end
  args = [:hrandfield, key]
  args << count if count
  args << "WITHVALUES" if with_values
  parser = Pairify if with_values
  send_command(args, &parser)
end

def hscan(key, cursor, **options)

Returns:
  • (String, Array<[String, String]>) - the next cursor and all found keys

Parameters:
  • options (Hash) --
  • cursor (String, Integer) -- the cursor of the iteration

Other tags:
    Example: Retrieve the first batch of key/value pairs in a hash -
def hscan(key, cursor, **options)
  _scan(:hscan, cursor, [key], **options) do |reply|
    [reply[0], reply[1].each_slice(2).to_a]
  end
end

def hscan_each(key, **options, &block)

Returns:
  • (Enumerator) - an enumerator for all found keys

Parameters:
  • options (Hash) --

Other tags:
    Example: Retrieve all of the key/value pairs in a hash -
def hscan_each(key, **options, &block)
  return to_enum(:hscan_each, key, **options) unless block_given?
  cursor = 0
  loop do
    cursor, values = hscan(key, cursor, **options)
    values.each(&block)
    break if cursor == "0"
  end
end

def hset(key, *attrs)

Returns:
  • (Integer) - The number of fields that were added to the hash

Parameters:
  • attrs (Array | Hash) -- array or hash of fields and values
  • key (String) --
def hset(key, *attrs)
  attrs = attrs.first.flatten if attrs.size == 1 && attrs.first.is_a?(Hash)
  send_command([:hset, key, *attrs])
end

def hsetnx(key, field, value)

Returns:
  • (Boolean) - whether or not the field was **added** to the hash

Parameters:
  • value (String) --
  • field (String) --
  • key (String) --
def hsetnx(key, field, value)
  send_command([:hsetnx, key, field, value], &Boolify)
end

def hvals(key)

Returns:
  • (Array) -

Parameters:
  • key (String) --
def hvals(key)
  send_command([:hvals, key])
end

def mapped_hmget(key, *fields)

Other tags:
    See: #hmget -

Returns:
  • (Hash) - a hash mapping the specified fields to their values

Parameters:
  • fields (Array) -- array of fields
  • key (String) --
def mapped_hmget(key, *fields)
  fields.flatten!(1)
  hmget(key, fields) do |reply|
    if reply.is_a?(Array)
      Hash[fields.zip(reply)]
    else
      reply
    end
  end
end

def mapped_hmset(key, hash)

Other tags:
    See: #hmset -

Returns:
  • (String) - `"OK"`

Parameters:
  • hash (Hash) -- a non-empty hash with fields mapping to values
  • key (String) --
def mapped_hmset(key, hash)
  hmset(key, hash.flatten)
end