Class: RPiet::Cycle

Inherits:
Object
  • Object
show all
Defined in:
lib/rpiet/cycle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, name, list) ⇒ Cycle

Returns a new instance of Cycle.



5
6
7
# File 'lib/rpiet/cycle.rb', line 5

def initialize(value, name, list)
  @value, @name, @list = value, name.to_s.downcase, list
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rpiet/cycle.rb', line 3

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/rpiet/cycle.rb', line 3

def value
  @value
end

Instance Method Details

#+(other) ⇒ Object



17
18
19
# File 'lib/rpiet/cycle.rb', line 17

def +(other)
  @list[(@value + other.value) % @list.length]
end

#-(other) ⇒ Object



13
14
15
# File 'lib/rpiet/cycle.rb', line 13

def -(other)
  @list[(@value - other.value) % @list.length]
end

#decr(amount = 1) ⇒ Object



25
26
27
# File 'lib/rpiet/cycle.rb', line 25

def decr(amount = 1)
  @list[(@value - amount) % @list.length]
end

#delta(other) ⇒ Object



9
10
11
# File 'lib/rpiet/cycle.rb', line 9

def delta(other)
  (@value - other.value) % @list.length      
end

#incr(amount = 1) ⇒ Object



21
22
23
# File 'lib/rpiet/cycle.rb', line 21

def incr(amount = 1)
  @list[(@value + amount) % @list.length]
end

#to_initialObject



29
30
31
# File 'lib/rpiet/cycle.rb', line 29

def to_initial
  @name[0]
end

#to_sObject Also known as: inspect



33
34
35
# File 'lib/rpiet/cycle.rb', line 33

def to_s
  "#{@name}(#{@value})"
end