Class: ArrayScanner
- Inherits:
-
Object
- Object
- ArrayScanner
- Defined in:
- lib/array_scanner.rb,
lib/array_scanner/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#pos_hist ⇒ Object
readonly
Returns the value of attribute pos_hist.
-
#position ⇒ Object
(also: #pos, #pointer)
Returns the value of attribute position.
-
#res_hist ⇒ Object
readonly
Returns the value of attribute res_hist.
Instance Method Summary collapse
- #current_element ⇒ Object (also: #points_at, #current)
- #eoa ⇒ Object
- #eoa? ⇒ Boolean
- #find ⇒ Object (also: #next)
- #forward(fixnum) ⇒ Object
- #forward_to ⇒ Object
-
#initialize(arr, history_size = 10) ⇒ ArrayScanner
constructor
A new instance of ArrayScanner.
- #inspect ⇒ Object
- #last_position ⇒ Object
- #last_positions(n = nil) ⇒ Object
- #last_result(valid = false) ⇒ Object
- #last_results(n = nil) ⇒ Object
- #look_behind(n = nil) ⇒ Object
- #look_behind_until ⇒ Object
- #needs_block ⇒ Object
- #peek(n = nil) ⇒ Object
- #peek_until ⇒ Object
- #previous ⇒ Object
- #reset ⇒ Object
- #rest ⇒ Object
- #rest_size ⇒ Object
- #rewind(fixnum) ⇒ Object
- #rewind_to ⇒ Object
- #rr(obj) ⇒ Object
- #scan(forward = true) ⇒ Object
- #scan_until(include_true_element = false) ⇒ Object
- #scanned ⇒ Object
- #scanned_size ⇒ Object
- #size ⇒ Object (also: #length)
- #surroundings ⇒ Object
- #terminate ⇒ Object (also: #clear)
- #to_a ⇒ Object
- #to_s ⇒ Object
- #unscan(steps = 1) ⇒ Object
Constructor Details
#initialize(arr, history_size = 10) ⇒ ArrayScanner
Returns a new instance of ArrayScanner.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/array_scanner.rb', line 9 def initialize(arr, history_size = 10) raise TypeError.new("Argument is not an Array.") unless arr.is_a?(Array) raise TypeError.new("History size is not a Fixnum.") unless history_size.is_a?(Fixnum) @arr = arr @position = 0 @pos_hist = Hist.new(history_size) @res_hist = Hist.new(history_size) end |
Instance Attribute Details
#pos_hist ⇒ Object (readonly)
Returns the value of attribute pos_hist.
5 6 7 |
# File 'lib/array_scanner.rb', line 5 def pos_hist @pos_hist end |
#position ⇒ Object Also known as: pos, pointer
Returns the value of attribute position.
5 6 7 |
# File 'lib/array_scanner.rb', line 5 def position @position end |
#res_hist ⇒ Object (readonly)
Returns the value of attribute res_hist.
5 6 7 |
# File 'lib/array_scanner.rb', line 5 def res_hist @res_hist end |
Instance Method Details
#current_element ⇒ Object Also known as: points_at, current
34 35 36 |
# File 'lib/array_scanner.rb', line 34 def current_element @arr[@position] end |
#eoa ⇒ Object
26 27 28 |
# File 'lib/array_scanner.rb', line 26 def eoa size - 1 end |
#eoa? ⇒ Boolean
30 31 32 |
# File 'lib/array_scanner.rb', line 30 def eoa? @position == eoa end |
#find ⇒ Object Also known as: next
185 186 187 188 189 190 191 |
# File 'lib/array_scanner.rb', line 185 def find if block_given? rest.find { |e| yield e } else raise needs_block end end |
#forward(fixnum) ⇒ Object
77 78 79 80 |
# File 'lib/array_scanner.rb', line 77 def forward(fixnum) new = @position + fixnum self.position = (new > eoa ? eoa : new) end |
#forward_to ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/array_scanner.rb', line 82 def forward_to if block_given? f = rest.find { |x| yield(x) } f ? self.position = @arr.index(f) : nil else raise needs_block end end |
#inspect ⇒ Object
221 222 223 |
# File 'lib/array_scanner.rb', line 221 def inspect "Pointer at #{@position}/#{eoa}. Current element: #{":" if current.is_a?(Symbol)}#{current}" end |
#last_position ⇒ Object
56 57 58 |
# File 'lib/array_scanner.rb', line 56 def last_position @pos_hist.recent end |
#last_positions(n = nil) ⇒ Object
60 61 62 |
# File 'lib/array_scanner.rb', line 60 def last_positions(n = nil) n ? @pos_hist.recent(n) : @pos_hist.stack end |
#last_result(valid = false) ⇒ Object
69 70 71 |
# File 'lib/array_scanner.rb', line 69 def last_result(valid = false) valid ? @res_hist.stack.find { |x| x } : @res_hist.recent end |
#last_results(n = nil) ⇒ Object
73 74 75 |
# File 'lib/array_scanner.rb', line 73 def last_results(n = nil) n ? @res_hist.recent(n) : @res_hist.stack end |
#look_behind(n = nil) ⇒ Object
171 172 173 |
# File 'lib/array_scanner.rb', line 171 def look_behind(n = nil) n ? scanned.reverse.take(n) : scanned.last end |
#look_behind_until ⇒ Object
175 176 177 178 179 180 181 182 183 |
# File 'lib/array_scanner.rb', line 175 def look_behind_until if block_given? rev = scanned.reverse i = rev.index { |el| yield(el) } i ? rev[0...i] : [] else raise needs_block end end |
#needs_block ⇒ Object
209 210 211 |
# File 'lib/array_scanner.rb', line 209 def needs_block ArgumentError.new("Method needs block.") end |
#peek(n = nil) ⇒ Object
158 159 160 |
# File 'lib/array_scanner.rb', line 158 def peek(n = nil) n ? rest.take(n) : rest.first end |
#peek_until ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/array_scanner.rb', line 162 def peek_until if block_given? i = rest.index { |el| yield(el) } i ? rest[0...i] : [] else raise needs_block end end |
#previous ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/array_scanner.rb', line 195 def previous if block_given? scanned.reverse.find { |e| yield e } else raise needs_block end end |
#reset ⇒ Object
105 106 107 |
# File 'lib/array_scanner.rb', line 105 def reset self.position = 0 end |
#rest ⇒ Object
123 124 125 |
# File 'lib/array_scanner.rb', line 123 def rest @arr[@position..-1] end |
#rest_size ⇒ Object
127 128 129 |
# File 'lib/array_scanner.rb', line 127 def rest_size size - @position end |
#rewind(fixnum) ⇒ Object
91 92 93 94 |
# File 'lib/array_scanner.rb', line 91 def rewind(fixnum) new = @position - fixnum self.position = (new < 0 ? 0 : new) end |
#rewind_to ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/array_scanner.rb', line 96 def rewind_to if block_given? b = scanned.find { |x| yield(x) } b ? self.position = @arr.index(b) : nil else raise needs_block end end |
#rr(obj) ⇒ Object
203 204 205 206 207 |
# File 'lib/array_scanner.rb', line 203 def rr(obj) # means return result @res_hist.push(obj) obj end |
#scan(forward = true) ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/array_scanner.rb', line 131 def scan(forward = true) res = @arr[@position] if block_given? and not yield(res) rr(false) else forward(1) if forward &! eoa? rr(res) end end |
#scan_until(include_true_element = false) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/array_scanner.rb', line 142 def scan_until(include_true_element = false) if block_given? if e = rest.find { |el| yield(el) } i = @arr.index(e) i += 1 if include_true_element self.position = (i > eoa ? eoa : i) rr(@arr[last_position...i]) else rr(false) end else raise needs_block end end |
#scanned ⇒ Object
115 116 117 |
# File 'lib/array_scanner.rb', line 115 def scanned @arr[0...@position] end |
#scanned_size ⇒ Object
119 120 121 |
# File 'lib/array_scanner.rb', line 119 def scanned_size @position end |
#size ⇒ Object Also known as: length
20 21 22 |
# File 'lib/array_scanner.rb', line 20 def size @arr.size end |
#surroundings ⇒ Object
52 53 54 |
# File 'lib/array_scanner.rb', line 52 def surroundings [scanned.last, rest[1]] end |
#terminate ⇒ Object Also known as: clear
109 110 111 |
# File 'lib/array_scanner.rb', line 109 def terminate self.position = eoa end |
#to_a ⇒ Object
213 214 215 |
# File 'lib/array_scanner.rb', line 213 def to_a @arr end |
#to_s ⇒ Object
217 218 219 |
# File 'lib/array_scanner.rb', line 217 def to_s @arr.to_s end |
#unscan(steps = 1) ⇒ Object
64 65 66 67 |
# File 'lib/array_scanner.rb', line 64 def unscan(steps = 1) return nil if last_positions.empty? self.position = last_positions[steps - 1] || last_positions.last end |