Class: Mext::EndlessArray
- Includes:
- Utilities
- Defined in:
- lib/mext/array/endless_array.rb
Overview
Mext::EndlessArray
an array
with an internal index which will chug out the next
or the previous value in an endless fashion (i.e. mod’ding the current index)
Direct Known Subclasses
Defined Under Namespace
Classes: RestartValueTooLarge
Constant Summary
Constants inherited from Array
Array::DOT_OPERATIONS, Array::MINIMUM_STEP, Array::SCALAR_NEW_OPERATIONS, Array::SCALAR_OPERATIONS
Instance Attribute Summary collapse
-
#cur ⇒ Object
readonly
Returns the value of attribute cur.
-
#restart_value ⇒ Object
Returns the value of attribute restart_value.
Class Method Summary collapse
Instance Method Summary collapse
-
#current ⇒ Object
current
. -
#initialize(me = [], rv = 0) ⇒ EndlessArray
constructor
Mext::EndlessArray.new(me = [], rv = 0).
-
#next(step = 1) ⇒ Object
next(step = 1).
-
#peek_next(step = 1) ⇒ Object
peek_next(step = 1).
-
#previous(step = 1) ⇒ Object
previous(step = 1).
-
#reset ⇒ Object
reset
.
Methods included from Utilities
Methods inherited from Array
Constructor Details
#initialize(me = [], rv = 0) ⇒ EndlessArray
Mext::EndlessArray.new(me = [], rv = 0)
create an endless array with a regular array copy. Another optional argument is the restart_value
(which defaults to 0)
21 22 23 24 |
# File 'lib/mext/array/endless_array.rb', line 21 def initialize(me = [], rv = 0) self.replace(me) @cur = @restart_value = rv end |
Instance Attribute Details
#cur ⇒ Object (readonly)
Returns the value of attribute cur.
12 13 14 |
# File 'lib/mext/array/endless_array.rb', line 12 def cur @cur end |
#restart_value ⇒ Object
Returns the value of attribute restart_value.
12 13 14 |
# File 'lib/mext/array/endless_array.rb', line 12 def restart_value @restart_value end |
Class Method Details
.from_yaml(ha) ⇒ Object
101 102 103 104 105 |
# File 'lib/mext/array/endless_array.rb', line 101 def from_yaml(ha) raise ArgumentError unless ha.kind_of?(Hash) && ha.has_key?('args') args = ha['args'].map { |value| value.kind_of?(String) ? eval(value) : value } new(args) end |
Instance Method Details
#current ⇒ Object
current
returns the value stored in the current location
82 83 84 |
# File 'lib/mext/array/endless_array.rb', line 82 def current self[self.cur] end |
#next(step = 1) ⇒ Object
next(step = 1)
bumps the current index forward of a step
value and returns the current element of array
43 44 45 46 47 |
# File 'lib/mext/array/endless_array.rb', line 43 def next(step = 1) res = self.current @cur = next_index(step) res end |
#peek_next(step = 1) ⇒ Object
peek_next(step = 1)
checks the next value contained in the array without bumping the index
32 33 34 35 |
# File 'lib/mext/array/endless_array.rb', line 32 def peek_next(step = 1) idx = next_index(step) self[idx] end |
#previous(step = 1) ⇒ Object
previous(step = 1)
bumps the index backwards and returns the previous element of the array
55 56 57 58 59 60 61 |
# File 'lib/mext/array/endless_array.rb', line 55 def previous(step = 1) res = self.current @cur -= step cur_check = @cur - self.restart_value @cur = cur_check < 0 ? (cur_check % self.size) : @cur res end |
#reset ⇒ Object
reset
reset the current
pointer to the restart_value
and returns whatever happens to be in it.
92 93 94 95 |
# File 'lib/mext/array/endless_array.rb', line 92 def reset @cur = self.restart_value self.current end |