Class: RubyLabs::BitLab::Message

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

Overview

Message

Message objects are arrays of Code objects.

There are two types of messages: packed and unpacked. If the message is unpacked, each code is stored in its own array element. This is the standard form for messages created with the :ascii or :parity encodings.

If the message is packed, codes are repackaged into 8-bit bytes, and individual codes may cross array item boundaries. This form is used by the method that uses a Huffman code to encode a string. #– TODO: allow for the possibility that a code word being added to a packed code can be longer than 8 bits

Constant Summary collapse

@@packsize =

number of bits to pack into single code

8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Message

Create a new Message of the specified type (:packed or :unpacked).

The message is initially empty; new characters are added by with the << operator.



798
799
800
801
802
803
804
805
806
807
# File 'lib/bitlab.rb', line 798

def initialize(type)
  raise "Message: unknown type" unless [:packed, :unpacked].include? type
  if type == :packed
    @packed = true
    @array = [ Code.new(0,0) ]
  else
    @packed = false
    @array = [ ]
  end
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



791
792
793
# File 'lib/bitlab.rb', line 791

def array
  @array
end

#packedObject

Returns the value of attribute packed.



791
792
793
# File 'lib/bitlab.rb', line 791

def packed
  @packed
end

Instance Method Details

#<<(x) ⇒ Object

Append the bits in Code object x to the end of this message.

Example:

>> m = Message.new(:packed)
=> 
>> m << Code.new(4)
=> 100
>> m << Code.new(3)
=> 10011


850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/bitlab.rb', line 850

def <<(x)
  raise "Message#<<: not a code" unless x.class == Code 
  if @packed
    if @array[-1].length + x.length <= @@packsize
      @array[-1] << x
    else
      n = @@packsize - @array[-1].length
      m = x.length - n
      @array[-1] << x[0...n] 
      @array << x[n...x.length]
    end
  else
    @array << x
  end
  return self
end

#copyObject

Create a new Message object that is a copy of this message.



811
812
813
814
815
816
# File 'lib/bitlab.rb', line 811

def copy
  dup = self.clone                              # copies @packed
  dup.array = Array.new
  @array.each { |x| dup.array << x.clone }      # deep copy of @array
  return dup
end

#each_bitObject

Iterate over each bit in a message, without regard for code boundaries.

Example:

>> m
=> 10011
>> m.each_bit { |b| puts b }
1
0
0
1
1
=> 10011


831
832
833
834
835
836
837
838
# File 'lib/bitlab.rb', line 831

def each_bit
  @array.each do |byte|
    for i in 0...byte.length
      yield(byte[i])
    end
  end
  return self
end

#inspectObject Also known as: to_s

Create a string of binary digits representing the Codes in this message.



895
896
897
898
899
900
901
# File 'lib/bitlab.rb', line 895

def inspect
  if @packed
    return @array.join("")
  else
    return @array.join(" ")
  end    
end

#lengthObject

Return the length (in bits) of this message.



889
890
891
# File 'lib/bitlab.rb', line 889

def length
  @array.inject(0) { |sum, code| sum += code.length }
end