Class: Brainfucktt::Byte

Inherits:
Object
  • Object
show all
Includes:
ConversionHelpers, Comparable
Defined in:
lib/brainfucktt/byte.rb

Overview

A Byte.

Instance Method Summary collapse

Constructor Details

#initialize(value = 0) ⇒ Byte

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

Parameters:

  • value (Byte, Integer, #to_i) (defaults to: 0)

Raises:



14
15
16
# File 'lib/brainfucktt/byte.rb', line 14

def initialize(value=0)
  @value = convert_to_integer(value)
end

Instance Method Details

#+(value) ⇒ Integer

Add to this Byte instance’s value.

Parameters:

Returns:

  • (Integer)

    The new value.

Raises:



23
24
25
# File 'lib/brainfucktt/byte.rb', line 23

def +(value)
  @value += convert_to_integer(value)
end

#-(value) ⇒ Integer

Subtract from this Byte instance’s value.

Parameters:

Returns:

  • (Integer)

    The new value.

Raises:



32
33
34
# File 'lib/brainfucktt/byte.rb', line 32

def -(value)
  @value -= convert_to_integer(value)
end

#<=>(other) ⇒ nil, Integer

Compare with another Byte, Integer, String, etc..

Returns:

  • (nil, Integer)


39
40
41
# File 'lib/brainfucktt/byte.rb', line 39

def <=>(other)
  @value <=> convert_to_integer(other)
end

#==(other) ⇒ true, false

Check to see if the given object instance is the same instance as self.

Returns:

  • (true, false)


46
47
48
# File 'lib/brainfucktt/byte.rb', line 46

def ==(other)
  self == other
end

#empty?true, false

Check this Byte instance is empty.

Returns:

  • (true, false)


62
63
64
# File 'lib/brainfucktt/byte.rb', line 62

def empty?
  @value == 0
end

#eql?(other) ⇒ true, false Also known as: equal?

Check to see if the given object instance as an Integer is the same as the value of this Byte instance.

Returns:

  • (true, false)


54
55
56
# File 'lib/brainfucktt/byte.rb', line 54

def eql?(other)
  @value == convert_to_integer(other)
end

#inspectString

Return the Byte as a binary string.

Returns:

  • (String)


98
99
100
# File 'lib/brainfucktt/byte.rb', line 98

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} integer=#{to_i.inspect} hex=#{to_hex.inspect} ascii=#{to_ascii.inspect} binary=#{to_binary.inspect} >"
end

#to_asciiString Also known as: to_s

Return the Byte as an ASCII character.

Returns:

  • (String)


83
84
85
# File 'lib/brainfucktt/byte.rb', line 83

def to_ascii
  @value.chr
end

#to_binaryString

Return the Byte as a binary string.

Returns:

  • (String)


76
77
78
# File 'lib/brainfucktt/byte.rb', line 76

def to_binary
  @value.to_s(2)
end

#to_hexString

Return the Byte as a hexadecimal string.

Returns:

  • (String)


91
92
93
# File 'lib/brainfucktt/byte.rb', line 91

def to_hex
  @value.to_s(16)
end

#to_iInteger

Return the Byte as an Integer

Returns:

  • (Integer)


69
70
71
# File 'lib/brainfucktt/byte.rb', line 69

def to_i
  @value
end