Topic: Ruby and reading individual bits from a binary file

I am attempting to read individual bytes (works fine) and then when it is a 'bool' read an individual bit from the file.  How does one do this?

def dump_binary type, length
  case type
  when 'longint'
    value = @binary_file.read(length).unpack("L").first.to_i
  when 'int'
    value = @binary_file.read(length).unpack("S").first.to_i
  when 'str'
    value = @binary_file.read(length).unpack("L").first.to_s
  when 'bool'
    value = @binary_file.read(length).unpack("B").first.to_s
  end
  return value
end

Re: Ruby and reading individual bits from a binary file

In the end, I simply converted the byte into a BitArray string and parsed each byte thereafter to come up with the bit settings and therefore the represented booleans.  Works fine now.

  #Method for parsing the various datatypes from the ECH file
  def dump_binary type, length
    case type
    when 'int'
      #Process integers, assigning appropriate profile based on length
      #such as long int, short int and tiny int.
      case length
      when 4
        value = @binary_file.read(length).unpack("l").first.to_i
      when 2
        value = @binary_file.read(length).unpack("s").first.to_i
      when 1
        value = @binary_file.read(length).unpack("U").first.to_i
      end
    #Process appropriate intergers into datetime format in the database
    when 'datetime'
      case length
      when 4
        value = @binary_file.read(length).unpack("l").first.to_i
      end
    #Process strings
    when 'str'
      value = @binary_file.read(length).unpack("M").first.to_s.rstrip
    #Process individual bits that are booleans
    when 'bool'
      value = @binary_file.read(length).unpack("b8").last.to_s
    #Process that one wierd boolean that is actually an int, instead of a bit
    when 'bool_int'
      value = @binary_file.read(length).unpack("U").first.to_i
    end
    return value
  end