Class: Redis::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Helpers::CoreCommands, Helpers::Serialize
Defined in:
lib/redis/set.rb

Overview

Class representing a set.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Serialize

#from_redis, #to_redis

Methods included from Helpers::CoreCommands

#exists?, #expire, #expireat, #move, #rename, #renamenx, #type

Constructor Details

#initialize(key, *args) ⇒ Set

Create a new Set.



16
17
18
19
20
# File 'lib/redis/set.rb', line 16

def initialize(key, *args)
  @key = key
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @redis = args.first || $redis
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/redis/set.rb', line 13

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/redis/set.rb', line 13

def options
  @options
end

#redisObject (readonly)

Returns the value of attribute redis.



13
14
15
# File 'lib/redis/set.rb', line 13

def redis
  @redis
end

Instance Method Details

#<<(value) ⇒ Object

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



23
24
25
26
# File 'lib/redis/set.rb', line 23

def <<(value)
  add(value)
  self  # for << 'a' << 'b'
end

#==(x) ⇒ Object



152
153
154
# File 'lib/redis/set.rb', line 152

def ==(x)
  members == x
end

#add(value) ⇒ Object

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



30
31
32
# File 'lib/redis/set.rb', line 30

def add(value)
  redis.sadd(key, to_redis(value))
end

#delete(value) ⇒ Object

Delete the value from the set. Redis: SREM



47
48
49
# File 'lib/redis/set.rb', line 47

def delete(value)
  redis.srem(key, to_redis(value))
end

#delete_if(&blk) ⇒ Object

Delete if matches block



52
53
54
55
56
57
58
59
60
# File 'lib/redis/set.rb', line 52

def delete_if(&blk)
  res = false
  redis.smembers(key).each do |m|
    if blk.call(from_redis(m))
      res = redis.srem(key, m)
    end
  end
  res
end

#difference(*sets) ⇒ Object Also known as: diff, ^, -

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



127
128
129
# File 'lib/redis/set.rb', line 127

def difference(*sets)
  from_redis redis.sdiff(key, *keys_from_objects(sets))
end

#diffstore(name, *sets) ⇒ Object

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



136
137
138
# File 'lib/redis/set.rb', line 136

def diffstore(name, *sets)
  redis.sdiffstore(name, key, *keys_from_objects(sets))
end

#each(&block) ⇒ Object

Iterate through each member of the set. Redis::Objects mixes in Enumerable, so you can also use familiar methods like collect, detect, and so forth.



64
65
66
# File 'lib/redis/set.rb', line 64

def each(&block)
  members.each(&block)
end

#empty?Boolean

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

Returns:

  • (Boolean)


148
149
150
# File 'lib/redis/set.rb', line 148

def empty?
  length == 0
end

#intersection(*sets) ⇒ Object Also known as: intersect, inter, &

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



79
80
81
# File 'lib/redis/set.rb', line 79

def intersection(*sets)
  from_redis redis.sinter(key, *keys_from_objects(sets))
end

#interstore(name, *sets) ⇒ Object

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



88
89
90
# File 'lib/redis/set.rb', line 88

def interstore(name, *sets)
  redis.sinterstore(name, key, *keys_from_objects(sets))
end

#lengthObject Also known as: size, count

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



141
142
143
# File 'lib/redis/set.rb', line 141

def length
  redis.scard(key)
end

#member?(value) ⇒ Boolean Also known as: include?

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/redis/set.rb', line 41

def member?(value)
  redis.sismember(key, to_redis(value))
end

#membersObject Also known as: get

Return all members in the set. Redis: SMEMBERS



35
36
37
# File 'lib/redis/set.rb', line 35

def members
  from_redis redis.smembers(key)
end

#to_sObject



156
157
158
# File 'lib/redis/set.rb', line 156

def to_s
  members.join(', ')
end

#union(*sets) ⇒ Object Also known as: |, +

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



103
104
105
# File 'lib/redis/set.rb', line 103

def union(*sets)
  from_redis redis.sunion(key, *keys_from_objects(sets))
end

#unionstore(name, *sets) ⇒ Object

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



111
112
113
# File 'lib/redis/set.rb', line 111

def unionstore(name, *sets)
  redis.sunionstore(name, key, *keys_from_objects(sets))
end