Class: Redis::Set
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.
-
#clear ⇒ Object
Wipe the set entirely.
-
#delete(name) ⇒ Object
Delete the value from the set.
-
#each(&block) ⇒ Object
Iterate through each member of the set.
-
#empty? ⇒ Boolean
Returns true if the set has no members.
-
#initialize(key, redis = $redis, options = {}) ⇒ Set
constructor
A new instance of Set.
-
#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
Constructor Details
#initialize(key, redis = $redis, options = {}) ⇒ Set
Returns a new instance of Set.
10 11 12 13 14 |
# File 'lib/redis/set.rb', line 10 def initialize(key, redis=$redis, ={}) @key = key @redis = redis @options = end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/redis/set.rb', line 9 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/redis/set.rb', line 9 def @options end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
9 10 11 |
# File 'lib/redis/set.rb', line 9 def redis @redis end |
Instance Method Details
#<<(value) ⇒ Object
Works like add. Can chain together: list << 'a' << 'b'
17 18 19 20 |
# File 'lib/redis/set.rb', line 17 def <<(value) add(value) self # for << 'a' << 'b' end |
#==(x) ⇒ Object
68 69 70 |
# File 'lib/redis/set.rb', line 68 def ==(x) members == x end |
#add(value) ⇒ Object
Add the specified value to the set only if it does not exist already. Redis: SADD
24 25 26 |
# File 'lib/redis/set.rb', line 24 def add(value) redis.sadd(key, value) end |
#clear ⇒ Object
Wipe the set entirely. Redis: DEL
47 48 49 |
# File 'lib/redis/set.rb', line 47 def clear redis.del(key) end |
#delete(name) ⇒ Object
Delete the value from the set. Redis: SREM
41 42 43 44 |
# File 'lib/redis/set.rb', line 41 def delete(name) redis.srem(key, name) get 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
64 65 66 |
# File 'lib/redis/set.rb', line 64 def empty? length == 0 end |
#length ⇒ Object Also known as: size
The number of members in the set. Aliased as size. Redis: SCARD
58 59 60 |
# File 'lib/redis/set.rb', line 58 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
35 36 37 |
# File 'lib/redis/set.rb', line 35 def member?(value) redis.sismember(key, value) end |
#members ⇒ Object Also known as: get
Return all members in the set. Redis: SMEMBERS
29 30 31 |
# File 'lib/redis/set.rb', line 29 def members redis.smembers(key) end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/redis/set.rb', line 72 def to_s members.join(', ') end |