Class: RubyLabs::BitLab::HexCode

Inherits:
Code
  • Object
show all
Defined in:
lib/bitlab.rb

Overview

HexCode

HexCodes are Code objects that are printed in hexadecimal. All of the attributes and methods of the Code class are applicable to HexCodes – the only differences are in to_s, which generates the string of digits used to print a number, and in the method that compares objects (Codes can only be compared to other Codes, HexCodes to other HexCodes).

Instance Attribute Summary

Attributes inherited from Code

#length, #value

Instance Method Summary collapse

Methods inherited from Code

#+, #<<, #[], #add_parity_bit, #chr, #even_parity?, #flip, #parity_bit

Constructor Details

#initialize(x, length = log2(x+1).ceil) ⇒ HexCode

Create a new HexCode object for the number x. The optional second argument specifies the number of bits to use in the encoding.



747
748
749
# File 'lib/bitlab.rb', line 747

def initialize(x, length = log2(x+1).ceil)
  super
end

Instance Method Details

#<=>(x) ⇒ Object

Compare the numeric values of this object and HexCode object x.



753
754
755
# File 'lib/bitlab.rb', line 753

def <=>(x)
  return x.class == HexCode && @value <=> x.value
end

#inspectObject Also known as: to_s

Return a string with the hexadecimal digits of the value represented by this Code object.



759
760
761
762
763
764
765
# File 'lib/bitlab.rb', line 759

def inspect
  if @length == 0
    return ""
  else
    return sprintf "%0#{@length/4}X", @value
  end
end