Class: Mext::EndlessArray

Inherits:
Array
  • Object
show all
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)

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

included

Methods inherited from Array

#choose, fill_float

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

#curObject (readonly)

Returns the value of attribute cur.



12
13
14
# File 'lib/mext/array/endless_array.rb', line 12

def cur
  @cur
end

#restart_valueObject

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

Raises:

  • (ArgumentError)


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

#currentObject

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

#resetObject

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