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.



745
746
747
# File 'lib/bitlab.rb', line 745

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.



751
752
753
# File 'lib/bitlab.rb', line 751

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.



757
758
759
760
761
762
763
# File 'lib/bitlab.rb', line 757

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