Ivan Kozlov Ivan Kozlov - 1 year ago 114
Ruby Question

How do I read/write binary files?

I've written a simple Huffman encoding in Ruby. As output I've got an array, for example:

["010", "1111", "10", "10", "110", "1110", "001", "110", "000", "10", "011"]

I need to write, and then read, it to and from a file. I tried several methods:

IO.binwrite("out.cake", array)

I get a simple text file and not binary.


File.open("out.cake", 'wb' ) do |output|
array.each do | byte |
output.print byte.chr

Which looks like it works, but then I can't read it into array.

Which encoding should I use?

Answer Source

I think you can just use Array#pack and String#unpack like the following code:

# Writing
a = ["010", "1111", "10", "10", "110", "1110", "001", "110", "000", "10", "011"]
File.open("out.cake", 'wb' ) do |output|
  output.write [a.join].pack("B*")

# Reading
s = File.binread("out.cake")
bits = s.unpack("B*")[0] # "01011111010110111000111000010011"

I don't know your preferred format for the result of reading and I know the above method is inefficient. But anyway you can take "0" or "1" sequentially from the result of unpack to traverse your Huffman tree.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download