Class: Seq
- Inherits:
-
Object
- Object
- Seq
- Includes:
- Enumerable
- Defined in:
- lib/seq.rb,
lib/seq/lazy.rb,
lib/seq/paged.rb,
lib/seq/random.rb,
lib/seq/version.rb
Overview
A Seq will cycle over a list returning a certain number of items.
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Method Summary collapse
-
#each ⇒ Object
Iterates over each item as returned by #next until #ended?.
-
#ended? ⇒ Boolean
Whether the Seq has returned enough items.
-
#entries ⇒ Array
The items that would be returned by repeated calls to #next until #ended?.
-
#inc ⇒ Integer
Increment the list index, the number of cycles completed and if at the end of the list returns to the first item.
-
#infinite? ⇒ Boolean
Whether the Seq returns infinite items.
-
#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) ⇒ Seq
constructor
Creates a new instance of Seq.
-
#method_missing(sym, *args, &block) ⇒ Object
Any method called on a Seq with ending with !, will be caught by method missing, it will then attempt to call the non ! version but will not alter the index or number of cycles completed.
-
#next ⇒ Object
Until ended it returns the next item from the list, when ended it returns the default item.
-
#reset ⇒ Object
Resets the Seqs position to the same as when initialized.
-
#to_a ⇒ Array
Returns the original list.
Constructor Details
#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) ⇒ Seq
Creates a new instance of Seq.
41 42 43 44 45 46 47 48 |
# File 'lib/seq.rb', line 41 def initialize(list=[], items=Float::INFINITY, offset=0, default=nil) @list = list @items = items @offset = offset @default = default self.reset end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Any method called on a Seq with ending with !, will be caught by method missing, it will then attempt to call the non ! version but will not alter the index or number of cycles completed.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/seq.rb', line 118 def method_missing(sym, *args, &block) if sym.to_s[-1] == "!" && self.respond_to?(sym.to_s[0..-2].to_sym) && ! [:infinite?, :ended?, :to_a, :entries, :inc, :reset].include?(sym.to_s[0..-2].to_sym) begin i, c = @index, @cycles self.send(sym.to_s[0..-2].to_sym, *args, &block) ensure @index, @cycles = i, c end else super end end |
Instance Method Details
#each ⇒ Object
Iterates over each item as returned by #next until #ended?.
80 81 82 83 84 |
# File 'lib/seq.rb', line 80 def each until ended? yield self.next end end |
#ended? ⇒ Boolean
Returns Whether the Seq has returned enough items.
111 112 113 |
# File 'lib/seq.rb', line 111 def ended? @cycles >= @items end |
#entries ⇒ Array
Returns The items that would be returned by repeated calls to #next until #ended?.
91 92 93 94 95 96 97 98 |
# File 'lib/seq.rb', line 91 def entries raise RangeError if infinite? i, c = @index, @cycles r = super @index, @cycles = i, c r end |
#inc ⇒ Integer
Increment the list index, the number of cycles completed and if at the end of the list returns to the first item.
70 71 72 73 74 75 76 77 |
# File 'lib/seq.rb', line 70 def inc if @index+1 == @list.size @index = 0 else @index += 1 end @cycles += 1 end |
#infinite? ⇒ Boolean
Returns Whether the Seq returns infinite items.
106 107 108 |
# File 'lib/seq.rb', line 106 def infinite? @items == Float::INFINITY end |
#next ⇒ Object
Returns Until ended it returns the next item from the list, when ended it returns the default item.
58 59 60 61 62 63 64 |
# File 'lib/seq.rb', line 58 def next if ended? @default else @list[@index].tap { inc } end end |
#reset ⇒ Object
Resets the Seqs position to the same as when initialized.
51 52 53 54 |
# File 'lib/seq.rb', line 51 def reset @cycles = 0 @index = @offset end |
#to_a ⇒ Array
Returns the original list
101 102 103 |
# File 'lib/seq.rb', line 101 def to_a @list end |