Class: Redis::Set
- Inherits:
-
Object
- Object
- 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.
-
#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.
-
#initialize(key, *args) ⇒ Set
constructor
Create a new Set.
-
#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)
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, #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
#key ⇒ Object (readonly)
Returns the value of attribute key.
13 14 15 |
# File 'lib/redis/set.rb', line 13 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/redis/set.rb', line 13 def @options end |
#redis ⇒ Object (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
140 141 142 |
# File 'lib/redis/set.rb', line 140 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, value) 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
116 117 118 |
# File 'lib/redis/set.rb', line 116 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
125 126 127 |
# File 'lib/redis/set.rb', line 125 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.
53 54 55 |
# File 'lib/redis/set.rb', line 53 def each(&block) members.each(&block) end |
#empty? ⇒ Boolean
Returns true if the set has no members. Redis: SCARD == 0
136 137 138 |
# File 'lib/redis/set.rb', line 136 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
68 69 70 |
# File 'lib/redis/set.rb', line 68 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
77 78 79 |
# File 'lib/redis/set.rb', line 77 def interstore(name, *sets) redis.sinterstore(name, key, *keys_from_objects(sets)) end |
#length ⇒ Object Also known as: size
The number of members in the set. Aliased as size. Redis: SCARD
130 131 132 |
# File 'lib/redis/set.rb', line 130 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
41 42 43 |
# File 'lib/redis/set.rb', line 41 def member?(value) redis.sismember(key, to_redis(value)) end |
#members ⇒ Object 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_s ⇒ Object
144 145 146 |
# File 'lib/redis/set.rb', line 144 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
92 93 94 |
# File 'lib/redis/set.rb', line 92 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
100 101 102 |
# File 'lib/redis/set.rb', line 100 def unionstore(name, *sets) redis.sunionstore(name, key, *keys_from_objects(sets)) end |