This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Processing a Binary File
CodeExpectedActual
open('binary', 'wb') do |f|
  (0..100).step(10) { |b| f << b.chr }
end
s = open('binary', 'rb') { |f| f.read } 
"\000\n\024\036(2<FPZd" "\000\n\024\036(2<FPZd"
open('binary', 'rb') { |f| f.each_byte { |b| puts b } }
0
10
20
...
90
100
0
10
20
30
40
50
60
70
80
90
100
f = open('binary')
f.pos
0 0
f.read(1)
"\000" "\000"
f.pos
1 1
f.pos = 4
4 4
f.read(2)
"(2" "(2"
f.pos
6 6
f.seek(8)                                    
f.pos
8 8
f.seek(-4, File::SEEK_CUR)
f.pos
4 4
f.seek(2, File::SEEK_CUR)
f.pos
6 6
f.seek(-2, File::SEEK_END)                                    
f.pos
9 9
f.read(500)
"Zd" "Zd"
f.pos
11 11
f.eof?
true true
f.close
f = open('binary', 'rb+')
f.read
"\000\n\024\036(2<FPZd" "\000\n\024\036(2<FPZd"
f.pos = 2
f.write('Hello.')
f.rewind
f.read
"\000\nHello.PZd" "\000\nHello.PZd"
f << 'Goodbye.'
f.rewind
f.read
"\000\nHello.PZdGoodbye." "\000\nHello.PZdGoodbye."
f.close
def parse_id3(mp3_file)
  fields_and_sizes = [[:track_name, 30], [:artist_name, 30], 
                      [:album_name, 30], [:year, 4], [:comment, 30], 
                      [:genre, 1]]
  tag = {}
  open(mp3_file) do |f|
    f.seek(-128, File::SEEK_END)
    if f.read(3) == 'TAG' # An ID3 tag is present
      fields_and_sizes.each do |field, size|
        # Read the field and strip off anything after the first null 
        # character.
        data = f.read(size).gsub(/\000.*/, '')
        # Convert the genre string to a number.
        data = data[0] if field == :genre
        tag[field] = data
      end
    end
  end
  return tag
end
parse_id3('ID3.mp3')
{:year=>"2005", :artist_name=>"The ID Three",
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - ID3.mp3
	from (irb):40:in `initialize'
	from (irb):40:in `parse_id3'
	from (irb):55
parse_id3('Too Indie For ID3 Tags.mp3')
{}
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - Too Indie For ID3 Tags.mp3
	from (irb):40:in `initialize'
	from (irb):40:in `parse_id3'
	from (irb):56
#Returns [track, artist, album, year, comment, genre]
def parse_id3(mp3_file)
  format = 'Z30Z30Z30Z4Z30C'
  open(mp3_file) do |f|
    f.seek(-128, File::SEEK_END)
    if f.read(3) == "TAG" # An ID3 tag is present
      return f.read(125).unpack(format)
    end
  end
  return nil
end
parse_id3('ID3.mp3')
["ID 3", "The ID Three", "Binary Brain Death", "2005", "http://www.example.com/id3/", 22]
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - ID3.mp3
	from (irb):60:in `initialize'
	from (irb):60:in `parse_id3'
	from (irb):68
id3 = ["ID 3", "The ID Three", "Binary Brain Death", "2005", 
       "http://www.example.com/id3/", 22]
id3.pack 'Z30Z30Z30Z4Z30C'
"ID 3\000\000\000\000\000...http://www.example.com/id3/\000\000\000\026" "ID 3\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000The ID Three\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000Binary Brain Death\000\000\000\000\000\000\000\000\000\000\000\0002005http://www.example.com/id3/\000\000\000\026"