Class: Redis::Set

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

Overview

Class representing a set.

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @key = key
  @redis = redis
  @options = options
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/redis/set.rb', line 9

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/redis/set.rb', line 9

def options
  @options
end

#redisObject (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

#clearObject

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

Returns:

  • (Boolean)


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

def empty?
  length == 0
end

#lengthObject 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

Returns:

  • (Boolean)


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

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

#membersObject 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_sObject



72
73
74
# File 'lib/redis/set.rb', line 72

def to_s
  members.join(', ')
end