Apple’s Leopard REXML Bug

Ruby, Ruby on Rails Add comments

Just in case any of you wonder why when you use REXML’s write method on ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0] and possibly other versions, you get errors, it’s a bug in Apple’s REXML library. See this:

def write(writer=$stdout, indent=-1, transitive=false, ie_hack=false)
  Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters")
  formatter = if indent > -1
    if transitive
      REXML::Formatters::Transitive.new( indent, ie_hack )
    else
      REXML::Formatters::Pretty.new( indent, ie_hack )
    end
  else
    REXML::Formatters::Default.new( ie_hack )
  end
  formatter.write( self, output )
end

As you can see, the method sets a local variable of ‘writer’ in the method arguments, however at the bottom it uses ‘output’. A simple change of the bottom line fixes it:

  formatter.write( self, writer )

For those who requested it, here’s how to use the formatter:

  # I want to create a REXML document to output
  require "rexml/document"
 
  # Make a pretty formatter
  formatter = REXML::Formatters::Pretty.new( 2 )
 
  # Create some silly xml example
  xml = REXML::Document.new '<moo>RAR!</moo>'
 
  # Write to STDOUT
  formatter.write( xml, $stdout )
 
  # Write to file
  xml_file = File.open( "some_file.xml", "a+" )
  formatter.write( xml, xml_file )

2 Responses to “Apple’s Leopard REXML Bug”

  1. tim beaumont Says:

    Hi Jamie,

    I thought I you might find this variation on “how many movies to connect to Kevin Bacon” interesting. I was born in London, UK; by parents from Yorkshire (Queensbury, Pontefract)before it split; raised in Canada and visited Skipton Fair as a child with relatives; married a Japanese; live in the USA, and found your site by a link from Honda Mihoko of NHK World Japanese Lessons to “mp3 japan”. Amazing, eh?

    By the way, how do i download the mp3’s?

    Tim

  2. Jamie van Dyke Says:

    Hah, nice…I like it! The Japanese files are available at http://japanese.fearoffish.com/, you can just right click on each and save them. I hope they help you.

    Jamie

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in