Rexml pretty printing "can't modify frozen string"

i want to use (ruby 1.9) Formatters::Pretty and i get the error :
/opt/local/lib/ruby1.9/1.9.1/rexml/formatters/pretty.rb:90:in
`squeeze!’: can’t modify frozen string (RuntimeError)

then, i look if my strings are frozen :

doc = Document.new(’’,{:raw=>:all})
[…]
fmt = Formatters::Pretty.new 2
s = String.new
puts s.frozen?
puts doc.frozen?
docs = doc.dup
puts docs.frozen?
fmt.write(docs, s)
puts s

for all s, doc and docs i get false to #.frozen?
what isn’t working here ?

Une Bévue wrote:

for all s, doc and docs i get false to #.frozen?
what isn’t working here ?

An object might not be frozen, but one of the objects it contains
(directly or indirectly) could be.

Looking at the source line which raised the error, the String which is
being squeezed is something returned by node.to_s

Interestingly, the line before does a gsub!, and it seems that this is
happy on a frozen string as long as no change is required:

s = “aaa”.freeze
=> “aaa”

s.gsub!(/b/,’’)
=> nil

This is different from the behaviour of squeeze!

s.squeeze!
TypeError: can’t modify frozen string
from (irb):17:in `squeeze!’
from (irb):17

Brian C. [email protected] wrote:

An object might not be frozen, but one of the objects it contains
(directly or indirectly) could be.

yes right !

Looking at the source line which raised the error, the String which is
being squeezed is something returned by node.to_s

then, no workaround ?

Une Bévue [email protected] wrote:

then, no workaround ?

I’ve found a working one, not using “REXML::Formatters::Pretty.new 2”,
see :
http://snippets.dzone.com/posts/show/4953 DZone Snippets - Pretty
Print XML using Ruby

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs