Hello,
I want to use the REXML class, but encounter the following problem, if I want to use an un-existant file:
file = File.new( "mydoc2.xml", "w+" ) # and this document does not exist
xml = Document.new(file)
I get the following error:
C:/ruby/lib/ruby/1.8/rexml/source.rb:128:in `initialize': undefined method `[]' for nil:NilClass (NoMethodError)
from C:/ruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:126:in `new'
from C:/ruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:126:in `stream='
from C:/ruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:100:in `initialize'
from C:/ruby/lib/ruby/1.8/rexml/parsers/treeparser.rb:8:in `new'
from C:/ruby/lib/ruby/1.8/rexml/parsers/treeparser.rb:8:in `initialize'
from C:/ruby/lib/ruby/1.8/rexml/document.rb:177:in `new'
from C:/ruby/lib/ruby/1.8/rexml/document.rb:177:in `build'
from C:/ruby/lib/ruby/1.8/rexml/document.rb:45:in `initialize'
from C:/Documents and Settings/Bart/My Documents/Programming/Ruby/Examples/XML/XMLTest1.rb:12:in `new'
from C:/Documents and Settings/Bart/My Documents/Programming/Ruby/Examples/XML/XMLTest1.rb:12
So I put on my 'gutty' shoes, and have a look, and saw that in the file C:/ruby/lib/ruby/1.8/rexml/source.rb, around 128, there is:
str = @source.read( 2 )
puts str, @source
if (str[0] == 254 && str[1] == 255) || (str[0] == 255 && str[1] == 254)
However, as @source is an empty file (I know this because of the puts), I get a nill as str, and hence, that error.
There are work-arounds for this (write to String and then to file), or maybe Im not supposed to do this, but it looked un-Ruby, at first. If the file is new, it should not try to parse it.
Just to let you know,
Bart.