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)


91
92
93
94
95
# File 'lib/mext/array/endless_array.rb', line 91

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



72
73
74
# File 'lib/mext/array/endless_array.rb', line 72

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



32
33
34
35
36
37
# File 'lib/mext/array/endless_array.rb', line 32

def next(step = 1)
  res = self.current
  @cur += step
  @cur = [self.cur % self.size, self.restart_value].max
  res
end

#previous(step = 1) ⇒ Object

previous(step = 1)

bumps the index backwards and returns the previous element of the array



45
46
47
48
49
50
51
# File 'lib/mext/array/endless_array.rb', line 45

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.



82
83
84
85
# File 'lib/mext/array/endless_array.rb', line 82

def reset
  @cur = self.restart_value
  self.current
end