Comment by flufluflufluffy

3 days ago

The phrase “Frozen String Literals” is kind of weird to me. When I assign a string literal to a variable, I do not think of the variable itself as a “string literal.” That phrase is for the literal characters in between quotes in the code, which by definition are already “frozen.” They’re a static part of the code itself. This change makes it so that you cannot mutate a variable which was initialized using a string literal. (if I understand correctly!)

That's not quite how Ruby and similar languages like Python or JS work.

Variables don't "contain" a string, they just point to objects on the heap.

So:

   my_string = same_string = "Hello World"

Here both variables are essentially pointers to a pre-existing object on the heap, and that object is immutable.

  • Yeah it’s just the naming is weird. The string literal is not the object on the heap, it’s part of the program’s code itself, which was (assumedly) never mutable to begin with.

    • "Frozen string literals" doesn't mean that string literals are frozen, but that string literals (in the code) are reified as frozen strings. Obviously, the feature name is less fully expressive than the full sentence used to explain it, but that's a natural trade-off with wanting a usable name; it is evocative of the definition, not a restatement of it.

In ruby, "frozen" is a property of some values that makes them immutable. I mean, other than the part where you can mutably unfreeze objects of many classes. (At least you can't unfreeze strings.) This change makes string values that come from literals initially frozen. It has nothing to do with variable bindings.