Class: Yarbf::BfInterpreter::BfCell
- Inherits:
-
Object
- Object
- Yarbf::BfInterpreter::BfCell
- Defined in:
- lib/yarbf.rb
Overview
Cell of the Brainfuck tape.
Instance Attribute Summary collapse
-
#cell_size ⇒ Object
Returns the value of attribute cell_size.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#decrease(decrement = 1, wrap_around = false) ⇒ Object
Decrease the value of a cell.
-
#increase(increment = 1, wrap_around = false) ⇒ Object
Increase the value of a cell.
-
#initialize(position, cell_size = 8, value = 0) ⇒ BfCell
constructor
A new instance of BfCell.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(position, cell_size = 8, value = 0) ⇒ BfCell
Returns a new instance of BfCell.
326 327 328 329 330 |
# File 'lib/yarbf.rb', line 326 def initialize(position, cell_size = 8, value = 0) @position = position @cell_size = cell_size @value = value end |
Instance Attribute Details
#cell_size ⇒ Object
Returns the value of attribute cell_size.
324 325 326 |
# File 'lib/yarbf.rb', line 324 def cell_size @cell_size end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
323 324 325 |
# File 'lib/yarbf.rb', line 323 def position @position end |
#value ⇒ Object
Returns the value of attribute value.
324 325 326 |
# File 'lib/yarbf.rb', line 324 def value @value end |
Instance Method Details
#decrease(decrement = 1, wrap_around = false) ⇒ Object
Decrease the value of a cell.
decrement-
Value to decrease by. Default is 1.
wrap_around-
Whether to wrap around. Default is false.
358 359 360 |
# File 'lib/yarbf.rb', line 358 def decrease(decrement = 1, wrap_around = false) self.increase(-decrement, wrap_around) end |
#increase(increment = 1, wrap_around = false) ⇒ Object
Increase the value of a cell.
increment-
Value to increase by. Default is 1.
wrap_around-
Whether to wrap around. Default is false.
343 344 345 346 347 348 349 350 |
# File 'lib/yarbf.rb', line 343 def increase(increment = 1, wrap_around = false) if !wrap_around && (@value + increment < 0 || @value + increment >= (1 << @cell_size)) fail 'Overflow or underflow happened while forbidden!' else @value = (@value + increment) % (1 << @cell_size) end end |
#inspect ⇒ Object Also known as: to_s
332 333 334 |
# File 'lib/yarbf.rb', line 332 def inspect "{ pos: #{@position}, value: #{@value} }" end |