Live data from Hacker News

Ruby 3.4 frozen string literals: What Rails developers need to know

prateekcodes.dev

51–60 of 131 posts

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#51
post #3

TIL that Ruby has mutable strings, and (until the announced change) even had them mutable by default (and the change only affects literal strings; non-literal strings are still mutable). Python has always only ever had immutable strings.

Strings will still be mutable by default after the change which only makes string literals always frozen (which has been a file-level opt-in for a while.)

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#52
post #21

Btw, OCaml also transitioned from read-write Strings to read-only Strings, and Buffer to be that read-write string. It was introduced in 4.02 released September 2014. I recall it was a bit bumpy, but not all that rough in the end. I suppose static type checking helps here to find all the ways how it could be used. There was a switch to allow running old code (to make strings and buffers interchangeable).

> Btw, OCaml also transitioned from read-write Strings to read-only Strings

Ruby is not doing that, it's transitioning from mutable strings that can be frozen with no special treatment of literals (unless you opt-in to literals being frozen on per file basis) to mutable strings with all string literals frozen.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#53
post #37

Earlier quoted context omitted.

Not in general. Immutable strings can be deduplicated, leading to a different performance tradeoff that is often quite good. This is mentioned in TFA.

Mutable strings can be duplicated too. You can use reference counting, or borrow checking in Rust.

Mutable string literals can't be easily deduplicated, unless your language semantics are that a literal is a singleton and all mutations are visible by all other evaluations of that literal. But no sane language would do that.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#54

We learned nothing from Python 2->3 An obviously good change, actually massive performance improvements not hard to implement but its still gonna be such a headache and dependency hell

Ruby has been extremely slow and deliberate in rolling out frozen string literals. They added a magic comment to opt in to them on a per-file basis all the way back in Ruby 2.3—almost a decade ago. https://www.ruby-lang.org/en/news/2015/12/25/ruby-2-3-0-rele... Most linting setups I've seen since then have required this line. I don’t expect many libraries to run afoul of this, and this warning setting will make findi…

I hope this is corect - i do agree it has been a long and slow migration path and migrating is fairly easy - migrating python 2 to 3 code was fairly easy as well anyone could do it in their codebase, it remains a big deal and possibly very impactful to make such breaking changes to the behavior of primitives in mature ecosystems. How many gems does the average rails app have, okay they all need to be updated and they sohld be being updated for other reasons, I remain skeptical of how smooth the change is going to be over all ecosystem wise but time will tell.

I agree it has been a well advertised and loudly migration path and timeframe for it

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#56

I wonder what the basis is for the description of the 3.7 / 4 ruby releases is. I haven't seen this transition plan with version numbers described outside of this blog post.

It's used as an example here: https://bugs.ruby-lang.org/issues/20205

But not actually stated it's the plan. I'd bet whatever LLM wrote the article took it as a stronger statement than it is.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#57
post #42
post #40

How does this work under the hood? Does Ruby keep a giant map of all strings in the application to check new strings against to see if it can dedupe? Does it keep a reference count to each unique string that requires a set lookup to update on each string instance’s deallocation? Set lookups in a giant set can be pretty expensive!

The literals would be identified at parse time. fooLit = "foo" fooVar = "f".concat("o").concat("o") This would have fooLit be frozen at parse time. In this situation there would be "foo", "f", and "o" as frozen strings; and fooLit and fooVar would be two different strings since fooVar was created at runtime. Creating a string that happens to be present in the frozen strings wouldn't create a new one.

Got it, so this could not be extended to non-literal strings

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#58
post #5
post #4

Is it the future path of any successful JIT / dynamic typed / scripting language to realize they needed all optimizations from compiled / statically typed / lower level languages ? Would Ruby be as successful if they had all those complicated features right from the start ? Or do all languages start from a nice simple clean slate tabula rasa to get developers hooked, until the language is enough famous to get well de…

I would actually say it’s the opposite in this case: it’s extremely common in scripting languages for strings to be immutable, mutable strings are usually only available in lower level languages. I’m very surprised Ruby had this feature at all.

Even in C, string literals are immutable, and mutating them is undefined behavior.

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#59
post #4

Is it the future path of any successful JIT / dynamic typed / scripting language to realize they needed all optimizations from compiled / statically typed / lower level languages ? Would Ruby be as successful if they had all those complicated features right from the start ? Or do all languages start from a nice simple clean slate tabula rasa to get developers hooked, until the language is enough famous to get well de…

> all optimizations from compiled / statically typed / lower level languages Mutable strings are totally possible (and not even especially hard) in compiled, statically typed, and lower-level languages. They're just not especially performant, and are sometimes a footgun. > all those complicated features right from the start Arguably, mutable strings are the more complicated feature. Removing them by default simplifie…

Its not about possible / not possible its about what the language does by default and how language syntax changes to switch between different variants of strings

Re: Ruby 3.4 frozen string literals: What Rails developers need to know

#60
post #5

Earlier quoted context omitted.

I would actually say it’s the opposite in this case: it’s extremely common in scripting languages for strings to be immutable, mutable strings are usually only available in lower level languages. I’m very surprised Ruby had this feature at all.

It’s true that many languages have immutable strings, but for a web-focused scripting language it makes sense to default to mutable strings. I think the comment is about that you now need to choose mutable vs immutable, and that is framed as a consequence of broader adoption. Which is a development I have also seen before.

Exactly thank you. Not sure why you’re downvoted
Post reply on HN