Class: Redis::Set
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::Set
- Includes:
- Enumerable, Helpers::CoreCommands, Helpers::Serialize
- Defined in:
- lib/redis/set.rb
Overview
Class representing a set.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Works like add.
- #==(x) ⇒ Object
-
#add(value) ⇒ Object
Add the specified value to the set only if it does not exist already.
-
#delete(value) ⇒ Object
Delete the value from the set.
-
#delete_if(&block) ⇒ Object
Delete if matches block.
-
#difference(*sets) ⇒ Object
(also: #diff, #^, #-)
Return the difference vs another set.
-
#diffstore(name, *sets) ⇒ Object
Calculate the diff and store it in Redis as
name
. -
#each(&block) ⇒ Object
Iterate through each member of the set.
-
#empty? ⇒ Boolean
Returns true if the set has no members.
-
#intersection(*sets) ⇒ Object
(also: #intersect, #inter, #&)
Return the intersection with another set.
-
#interstore(name, *sets) ⇒ Object
Calculate the intersection and store it in Redis as
name
. -
#length ⇒ Object
(also: #size, #count)
The number of members in the set.
-
#member?(value) ⇒ Boolean
(also: #include?)
Returns true if the specified value is in the set.
-
#members ⇒ Object
(also: #get)
Return all members in the set.
- #to_s ⇒ Object
-
#union(*sets) ⇒ Object
(also: #|, #+)
Return the union with another set.
-
#unionstore(name, *sets) ⇒ Object
Calculate the union and store it in Redis as
name
.
Methods included from Helpers::Serialize
Methods included from Helpers::CoreCommands
#exists?, #expire, #expireat, #move, #rename, #renamenx, #sort, #ttl, #type
Methods inherited from BaseObject
Constructor Details
This class inherits a constructor from Redis::BaseObject
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/redis/set.rb', line 15 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/redis/set.rb', line 15 def @options end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
15 16 17 |
# File 'lib/redis/set.rb', line 15 def redis @redis end |
Instance Method Details
#<<(value) ⇒ Object
Works like add. Can chain together: list << ‘a’ << ‘b’
18 19 20 21 |
# File 'lib/redis/set.rb', line 18 def <<(value) add(value) self # for << 'a' << 'b' end |
#==(x) ⇒ Object
148 149 150 |
# File 'lib/redis/set.rb', line 148 def ==(x) members == x end |
#add(value) ⇒ Object
Add the specified value to the set only if it does not exist already. Redis: SADD
25 26 27 |
# File 'lib/redis/set.rb', line 25 def add(value) redis.sadd(key, to_redis(value)) end |
#delete(value) ⇒ Object
Delete the value from the set. Redis: SREM
43 44 45 |
# File 'lib/redis/set.rb', line 43 def delete(value) redis.srem(key, to_redis(value)) end |
#delete_if(&block) ⇒ Object
Delete if matches block
48 49 50 51 52 53 54 55 56 |
# File 'lib/redis/set.rb', line 48 def delete_if(&block) res = false redis.smembers(key).each do |m| if block.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
123 124 125 |
# File 'lib/redis/set.rb', line 123 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
132 133 134 |
# File 'lib/redis/set.rb', line 132 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.
60 61 62 |
# File 'lib/redis/set.rb', line 60 def each(&block) members.each(&block) end |
#empty? ⇒ Boolean
Returns true if the set has no members. Redis: SCARD == 0
144 145 146 |
# File 'lib/redis/set.rb', line 144 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
75 76 77 |
# File 'lib/redis/set.rb', line 75 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
84 85 86 |
# File 'lib/redis/set.rb', line 84 def interstore(name, *sets) redis.sinterstore(name, key, *keys_from_objects(sets)) end |
#length ⇒ Object Also known as: size, count
The number of members in the set. Aliased as size. Redis: SCARD
137 138 139 |
# File 'lib/redis/set.rb', line 137 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
37 38 39 |
# File 'lib/redis/set.rb', line 37 def member?(value) redis.sismember(key, to_redis(value)) end |
#members ⇒ Object Also known as: get
Return all members in the set. Redis: SMEMBERS
30 31 32 33 |
# File 'lib/redis/set.rb', line 30 def members v = from_redis redis.smembers(key) v.nil? ? [] : v end |
#to_s ⇒ Object
152 153 154 |
# File 'lib/redis/set.rb', line 152 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
99 100 101 |
# File 'lib/redis/set.rb', line 99 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
107 108 109 |
# File 'lib/redis/set.rb', line 107 def unionstore(name, *sets) redis.sunionstore(name, key, *keys_from_objects(sets)) end |