# Rexml pretty printing "can't modify frozen string"

**URL:** <https://www.ruby-forum.com/t/rexml-pretty-printing-cant-modify-frozen-string/187542>\
**Category:** Ruby\
**Created:** [April 25, 2010, 10:55am UTC](https://www.ruby-forum.com/t/rexml-pretty-printing-cant-modify-frozen-string/187542 "2010-04-25T10:55:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [April 25, 2010, 10:55am UTC](https://www.ruby-forum.com/t/rexml-pretty-printing-cant-modify-frozen-string/187542/1 "2010-04-25T10:55:48Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [April 25, 2010, 11:20am UTC](https://www.ruby-forum.com/t/rexml-pretty-printing-cant-modify-frozen-string/187542/2 "2010-04-25T11:20:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [April 25, 2010, 11:50am UTC](https://www.ruby-forum.com/t/rexml-pretty-printing-cant-modify-frozen-string/187542/3 "2010-04-25T11:50:49Z")

</div>

Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ?

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [April 25, 2010, 12:31pm UTC](https://www.ruby-forum.com/t/rexml-pretty-printing-cant-modify-frozen-string/187542/4 "2010-04-25T12:31:13Z")

</div>

Une Bévue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://snippets.dzone.com/posts/show/4953) DZone Snippets - Pretty  
Print XML using Ruby
