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.

Converting Among Image Formats (written by Antonio Cangiano)
CodeExpectedActual
require 'rubygems'
require 'RMagick'
img = Magick::Image.read('myimage.png').first
img.write('myimage.jpg')
Magick::Image.read('JPG:myimage.png')
Magick::ImageMagickError: Not a JPEG file: starts with 0x89 0x50 `myimage.png': 
...
Magick::ImageMagickError: Not a JPEG file: starts with 0x89 0x50 `myimage.png': 
	from (irb):5:in `read'
	from (irb):5
	from :0
img = Magick::Image.read('myimage.png').first
img.write('myimage.jpg')          # Writes a JPEG
img.write('myimage.gif')          # Writes a GIF
img.write('JPG:myimage')          # Writes a JPEG
img.write('JPG:myimage.gif')      # Writes a JPEG
img.format
"PNG" "PNG"
img.format = 'GIF'
img.format
"GIF" "GIF"
Magick.formats['GIF']
"*rw+" "*rw+"
Magick.formats['JPG']
"*rw-" "*rw-"
Magick.formats['AVI']
"*r--" "*r--"
Magick.formats['PS']
" rw+" " rw+"
module Magick
 [['native_blob?', ?*], ['readable?', ?r],
  ['writable?', ?w], ['multi_image?', ?+]].each_with_index do |m, i|
   define_method(m[0]) do |format|
     code = formats[format]
     return code && code[i] == m[1]
   end
   module_function(m[0])
 end
end
Magick.multi_image? 'GIF'
true true
Magick.multi_image? 'JPG'
false false