Class: Brainfucktt::Data

Inherits:
Object
  • Object
show all
Includes:
ConversionHelpers
Defined in:
lib/brainfucktt/data.rb

Overview

An Array of Byte instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



12
13
14
# File 'lib/brainfucktt/data.rb', line 12

def initialize
  @bytes = []
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



10
11
12
# File 'lib/brainfucktt/data.rb', line 10

def bytes
  @bytes
end

Instance Method Details

#[](offset) ⇒ Byte

Retrieve the Byte instance at the given offset.

Parameters:

  • offset (Integer, #to_i)

Returns:

  • (Byte)

    The Byte instance at the given offset.

Raises:



21
22
23
24
25
26
# File 'lib/brainfucktt/data.rb', line 21

def [](offset)
  offset = convert_to_integer(offset)
  self[offset] = 0 if @bytes[offset].nil?
  
  @bytes[offset]
end

#[]=(offset, value) ⇒ Byte

Set the value of the Byte instance at the given offset.

Parameters:

  • offset (Integer, #to_i)
  • value (Byte, Integer, #to_i)

Returns:

  • (Byte)

    The Byte instance at the given offset.

Raises:



35
36
37
38
39
# File 'lib/brainfucktt/data.rb', line 35

def []=(offset, value)
  expand_to(offset)
  
  @bytes[offset] = convert_to_byte(value)
end

#expand_to(offset) ⇒ Byte

Fill from the end of the Data collection to the given offset with empty Byte instances, if Data collection is less than the given offset.

Parameters:

  • offset (Integer, #to_i)

Returns:

  • (Byte)

    The Byte instance at the given offset.

Raises:



47
48
49
50
51
52
# File 'lib/brainfucktt/data.rb', line 47

def expand_to(offset)
  offset = convert_to_integer(offset)
  (@bytes.length..offset).each { @bytes << Byte.new } if @bytes.at(offset).nil?
  
  @bytes[offset]
end