Getting can't modify frozen string when using string.insert

Posted by Richard on Stack Overflow See other posts from Stack Overflow or by Richard
Published on 2010-06-14T00:52:03Z Indexed on 2010/06/14 1:02 UTC
Read the original article Hit count: 249

Filed under:

Hi,

This is a quick question. I have the following ruby code, which works fine.

  def add_zeros number, zeros
    number = number.to_s
    zeros_to_add = zeros - number.length
    zeros_to_add.times do
      number = "0#{number}"
    end
    number
  end

But if I replace

number = "0#{number}"

With

number.insert(0, "0")

Then I get TypeError: can't modify frozen string, does anyone know why this is?

© Stack Overflow or respective owner

Related posts about ruby