9/30/2008

Pack Method in Ruby

"pack" method is to show us binary format of object in Ruby Program.

Recently I want to create binary data from text file with Ruby.
Ruby has powerful regexp and convenient methods to be equip.

Therefore I try to create file and to store binary data.
But, I wonder how to store binary data!

Ruby provides the way to store for file is only text format.
I am in trouble.

I study a reference of Ruby.
It describes following.

  • Firstly You must use "pack" of Array Class method in order to know text of binary format of object
  • Secondly You store text of binary format in file.


    Example code is next.

    file << [10].pack("i")
  • No comments: