Class: EasyRedis::Collection
- Inherits:
-
Object
- Object
- EasyRedis::Collection
- Includes:
- Enumerable
- Defined in:
- lib/easyredis.rb
Overview
class representing a generic collection
Direct Known Subclasses
Instance Method Summary collapse
-
#[](index, limit = nil) ⇒ Object
access elements in this sort.
-
#each ⇒ Object
iterate through all members of this collection.
-
#first(n = nil) ⇒ Object
return the fist element of this collection, or the first n elements, if n is given.
-
#last(n = nil) ⇒ Object
return the last element of this collection, or the last n elements, if n is given.
Instance Method Details
#[](index, limit = nil) ⇒ Object
access elements in this sort
Work’s like an Array’s [] method. It can take a specific index, a range, or an offset and an amount/limit. This method uses the underlying access method, which handles the actual retrival.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/easyredis.rb', line 120 def [](index,limit=nil) if limit offset = index self[offset...(offset+limit)] elsif index.is_a? Range access(index) elsif index.is_a? Integer self[index..index].first end end |
#each ⇒ Object
iterate through all members of this collection
132 133 134 |
# File 'lib/easyredis.rb', line 132 def each self[0..-1].each { |o| yield o } end |
#first(n = nil) ⇒ Object
return the fist element of this collection, or the first n elements, if n is given
137 138 139 140 141 142 143 |
# File 'lib/easyredis.rb', line 137 def first(n = nil) if n self[0,n] else self[0] end end |
#last(n = nil) ⇒ Object
return the last element of this collection, or the last n elements, if n is given
146 147 148 149 150 151 152 |
# File 'lib/easyredis.rb', line 146 def last(n = nil) if n self[-n..-1] else self[-1] end end |