class Redis::Set

Class representing a set.

Public Instance Methods

&(*sets)
Alias for: intersection
+(*sets)
Alias for: union
-(*sets)
Alias for: difference
<<(value) click to toggle source

Works like add. Can chain together: list << ‘a’ << ‘b’

# File lib/redis/set.rb, line 9
def <<(value)
  add(value)
  self  # for << 'a' << 'b'
end
==(x) click to toggle source
# File lib/redis/set.rb, line 167
def ==(x)
  members == x
end
^(*sets)
Alias for: difference
add(value) click to toggle source

Add the specified value to the set only if it does not exist already. Redis: SADD

# File lib/redis/set.rb, line 16
def add(value)
  allow_expiration do
    redis.sadd(key, marshal(value)) if value.nil? || !Array(value).empty?
  end
end
count()
Alias for: length
delete(value) click to toggle source

Delete the value from the set. Redis: SREM

# File lib/redis/set.rb, line 55
def delete(value)
  redis.srem(key, marshal(value))
end
delete_if(&block) click to toggle source

Delete if matches block

# File lib/redis/set.rb, line 60
def delete_if(&block)
  res = false
  redis.smembers(key).each do |m|
    if block.call(unmarshal(m))
      res = redis.srem(key, m)
    end
  end
  res
end
diff(*sets)
Alias for: difference
difference(*sets) click to toggle source

Return the difference vs another set. Can pass it either another set object or set name. Also available as ^ or - which is a bit cleaner:

members_difference = set1 ^ set2
members_difference = set1 - set2

If you want to specify multiple sets, you must use difference:

members_difference = set1.difference(set2, set3, set4)
members_difference = set1.diff(set2, set3, set4)

Redis: SDIFF

# File lib/redis/set.rb, line 129
def difference(*sets)
  redis.sdiff(key, *keys_from_objects(sets)).map{|v| unmarshal(v)}
end
Also aliased as: diff, ^, -
diffstore(name, *sets) click to toggle source

Calculate the diff and store it in Redis as name. Returns the number of elements in the stored union. Redis: SDIFFSTORE

# File lib/redis/set.rb, line 138
def diffstore(name, *sets)
  redis.sdiffstore(name, key, *keys_from_objects(sets))
end
empty?() click to toggle source

Returns true if the set has no members. Redis: SCARD == 0

# File lib/redis/set.rb, line 163
def empty?
  length == 0
end
get()
Alias for: members
include?(value)
Alias for: member?
inter(*sets)
Alias for: intersection
intersect(*sets)
Alias for: intersection
intersection(*sets) click to toggle source

Return the intersection with another set. Can pass it either another set object or set name. Also available as & which is a bit cleaner:

members_in_both = set1 & set2

If you want to specify multiple sets, you must use intersection:

members_in_all = set1.intersection(set2, set3, set4)
members_in_all = set1.inter(set2, set3, set4)  # alias

Redis: SINTER

# File lib/redis/set.rb, line 81
def intersection(*sets)
  redis.sinter(key, *keys_from_objects(sets)).map{|v| unmarshal(v)}
end
Also aliased as: intersect, inter, &
interstore(name, *sets) click to toggle source

Calculate the intersection and store it in Redis as name. Returns the number of elements in the stored intersection. Redis: SUNIONSTORE

# File lib/redis/set.rb, line 90
def interstore(name, *sets)
  redis.sinterstore(name, key, *keys_from_objects(sets))
end
length() click to toggle source

The number of members in the set. Aliased as size or count. Redis: SCARD

# File lib/redis/set.rb, line 156
def length
  redis.scard(key)
end
Also aliased as: size, count
member?(value) click to toggle source

Returns true if the specified value is in the set. Redis: SISMEMBER

# File lib/redis/set.rb, line 49
def member?(value)
  redis.sismember(key, marshal(value))
end
Also aliased as: include?
members() click to toggle source

Return all members in the set. Redis: SMEMBERS

# File lib/redis/set.rb, line 41
def members
  vals = redis.smembers(key)
  vals.nil? ? [] : vals.map{|v| unmarshal(v) }
end
Also aliased as: get, value
merge(*values) click to toggle source

Adds the specified values to the set. Only works on redis > 2.4 Redis: SADD

# File lib/redis/set.rb, line 34
def merge(*values)
  allow_expiration do
    redis.sadd(key, values.flatten.map{|v| marshal(v)})
  end
end
move(value, destination) click to toggle source

Moves value from one set to another. Destination can be a String or Redis::Set.

set.move(value, "name_of_key_in_redis")
set.move(value, set2)

Returns true if moved successfully.

Redis: SMOVE

# File lib/redis/set.rb, line 151
def move(value, destination)
  redis.smove(key, destination.is_a?(Redis::Set) ? destination.key : destination.to_s, value)
end
pop(count = nil) click to toggle source

Remove and return a random member. Redis: SPOP

# File lib/redis/set.rb, line 23
def pop(count = nil)
  unmarshal redis.spop(key, count)
end
randmember(count = nil) click to toggle source

return a random member. Redis: SRANDMEMBER

# File lib/redis/set.rb, line 28
def randmember(count = nil)
  unmarshal redis.srandmember(key, count)
end
size()
Alias for: length
to_s() click to toggle source
# File lib/redis/set.rb, line 171
def to_s
  members.join(', ')
end
union(*sets) click to toggle source

Return the union with another set. Can pass it either another set object or set name. Also available as | and + which are a bit cleaner:

members_in_either = set1 | set2
members_in_either = set1 + set2

If you want to specify multiple sets, you must use union:

members_in_all = set1.union(set2, set3, set4)

Redis: SUNION

# File lib/redis/set.rb, line 105
def union(*sets)
  redis.sunion(key, *keys_from_objects(sets)).map{|v| unmarshal(v)}
end
Also aliased as: |, +
unionstore(name, *sets) click to toggle source

Calculate the union and store it in Redis as name. Returns the number of elements in the stored union. Redis: SUNIONSTORE

# File lib/redis/set.rb, line 113
def unionstore(name, *sets)
  redis.sunionstore(name, key, *keys_from_objects(sets))
end
value()
Alias for: members
|(*sets)
Alias for: union

Private Instance Methods

keys_from_objects(sets) click to toggle source
# File lib/redis/set.rb, line 177
def keys_from_objects(sets)
  raise ArgumentError, "Must pass in one or more set names" if sets.empty?
  sets.collect{|set| set.is_a?(Redis::Set) ? set.key : set}
end