0

I'm running into an issue where my script is failing in random places with:

Error: can't modify frozen String: "Please use text available here - Test jira"

These lines caused the error:

description = "Please use text available here - #{@jira[:url]}"

unless previous_jira.nil?
  description << <<~PREVIOUSJIRACREATED
     Please close previous jira's:
      #{previous_jira}
  PREVIOUSJIRACREATED
end

I think it's a pretty simple line and I am not freezeing it on purpose or anything like that. But can't understand why I am getting the error.

I have string interpolation all over my code, and the script started failing randomly at different places. The above code is just one example. I was able to identify a couple of high offenders and placed begin/rescue blocks around them, but I am afraid my code is getting ugly with the rescue blocks all over.

I tried searching, but I only got articles explaining what freeze is and how to dup the object.

I am on Ruby 2.7.0p0.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
scorpion35
  • 944
  • 2
  • 12
  • 30
  • 3
    I don’t think that’s the problematic line. Where is the code where you _modify_ the string? Showing us just one line of code extracted from all context won’t get us any further. – matt Mar 23 '20 at 23:36
  • 1
    I suggest reading https://stackoverflow.com/q/37799296/3784008 – anothermh Mar 23 '20 at 23:38
  • @matt Thanks for the reply! I've added more code to the question. But that is it. I am declaring `description` there and trying to modify it in the next line based on a condition. – scorpion35 Mar 23 '20 at 23:39
  • @anothermh Bingo! My class file is so big I forgot I have this on top of the file - `# frozen_string_literal: true`. Thank you very much for identifying the issue! ‍♂️ But I have string interpolation all over the class. Not sure why it's `freeze`ing implicitly in some places, and not in others. And that too very randomly. Could be a bug in Ruby? – scorpion35 Mar 23 '20 at 23:42
  • 3
    lol no not a bug in Ruby. Let's not blame the underlying implementation of the language on an ill-defined problem in a single file. I recommend working on understanding how frozen strings are handled in Ruby to understand what's wrong with your specific code. – anothermh Mar 24 '20 at 00:08
  • 3
    Your expanded example shows exactly the issue: when you set `description = 'foo'` that string is frozen. In the next line you attempt to modify the frozen string with `<<` which isn't going to work. If you want to do that, use for example the [unary plus operator](https://stackoverflow.com/a/52929605/3784008) as in `description = +description << 'bar'` or string interpolation to reset the variable as in `description = "#{description}bar"`. – anothermh Mar 24 '20 at 00:15
  • 1
    "`Could be a bug in Ruby?`". I'd highly recommend reading at least the section "[Don't rush to claim that you have found a bug](http://catb.org/esr/faqs/smart-questions.html#idm368)" but the entire page is highly recommended. – the Tin Man Mar 24 '20 at 04:10
  • @anothermh Gotcha! Totally makes sense to replace the string with new one instead of appending when I said `frozen_string_literal` is `true. For now I turned `frozen_string_literal` to `false` and it's working as normal. Thanks again for your help! – scorpion35 Mar 24 '20 at 11:16
  • @theTinMan Thanks for the link! It's informative. But the point I am trying to make is, I have string init and append in a lots of places in the same class file, and also in a several other class files (which also have `frozen_string_literal: true`). I don't understand why I am seeing random failures in different places each time when all could've failed once? This is what's confusing me. Hope you understand! – scorpion35 Mar 24 '20 at 11:20
  • 1
    @scorpion35 That's exactly the point TinMan was making. You say you're confused and don't understand what's going on. So what's more likely, that Ruby has a bug, or that code that confuses you and that you don't fully understand has a bug? – anothermh Mar 24 '20 at 17:35
  • Your example code doesn't duplicate the problem you describe, so how are we supposed to help fix the problem? Please see "[ask]", "[Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648)" and "[MCVE](https://stackoverflow.com/help/minimal-reproducible-example)" and all their linked pages. The question _must_ have the minimal code that demonstrates the problem. It's not possible to try to determine every possible scenario where your tiny snippet could go wrong without the supporting code. – the Tin Man Mar 25 '20 at 00:16
  • @theTinMan Yes, exactly. That is the issue I am trying to describe. I had `frozen_string_literal: true` from the beginning and have a lot of string appends throughout my code. However, my script fails randomly at different string append places. My understanding is, if `frozen_string_literal: true`, it should NOT allow any type of string append anywhere in the class file. The code I posted in the question was not something I wrote like a min ago and then it started failing. I have the similar code in several other places. Confused why `frozen_string_literal: true` is failing some randoml. – scorpion35 Mar 26 '20 at 09:24
  • 2
    I'll bet $100 that it isn't failing randomly, but is instead failing predictably according to the rules around usage of frozen strings, and that given a proper code sample and stack traces for any errors from that code all errors can be adequately explained. – anothermh Mar 27 '20 at 03:07

0 Answers0