Rexml pretty printing "can't modify frozen string"

Discussion in 'Ruby' started by Une Bévue, Apr 25, 2010.

  1. Une Bévue

    Une Bévue Guest

    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('<html></html>',{: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 ?
    --
    « Il y a tellement de choses plus importantes dans la vie que l'argent,
    mais il faut tellement d'argent pour les acquérir. »
    (Groucho Marx)
     
    Une Bévue, Apr 25, 2010
    #1
    1. Advertising

  2. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Apr 25, 2010
    #2
    1. Advertising

  3. Une Bévue

    Une Bévue Guest

    Brian Candler <> 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 ?
    --
    « Il y a tellement de choses plus importantes dans la vie que l'argent,
    mais il faut tellement d'argent pour les acquérir. »
    (Groucho Marx)
     
    Une Bévue, Apr 25, 2010
    #3
  4. Une Bévue

    Une Bévue Guest

    Une Bévue <> 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
    --
    « Il y a tellement de choses plus importantes dans la vie que l'argent,
    mais il faut tellement d'argent pour les acquérir. »
    (Groucho Marx)
     
    Une Bévue, Apr 25, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damphyr
    Replies:
    2
    Views:
    141
    Damphyr
    Jul 16, 2003
  2. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    153
    Henrik Horneber
    Oct 12, 2004
  3. Peña, Botp
    Replies:
    2
    Views:
    175
    Peña, Botp
    May 31, 2007
  4. Phlip
    Replies:
    0
    Views:
    143
    Phlip
    Jan 15, 2008
  5. Charles Roper

    REXML::Formatters::Pretty wrap width?

    Charles Roper, Sep 18, 2009, in forum: Ruby
    Replies:
    0
    Views:
    138
    Charles Roper
    Sep 18, 2009
Loading...

Share This Page