It still feels like symbols are a hack due to mutability of Ruby strings, if they were immutable and only initialized once I don't see the need for symbols...
I took it a bit differently. By the time we get to
I think the first place you’d see symbols in Ruby are in hashmaps.
user = { "name" => "Alice", "age" => 30 } # String keys
user = { :name => "Alice", :age => 30 } # Symbol keys
I more get the impression that they're useful for slapping together ad-hoc, anonymous structs/dataclasses. Essentially it winds up being something similar to tuples, just with names for access rather than numbers.
How is that about mutability? Seems to me you'd rather need reference capabilities and/or string interning to avoid spawning as many strings as hashmaps
Because they're mutable means they cant re-use the "name" string and are forced to initialize 1000 of them...
Even in the case with a language with immutable strings, it's entirely possible to initialize 1000 identical strings in a loop. If you want to avoid that, you need string interning or references.
The mutability doesn't help for use as dict keys, but as far as I can tell, just having immutable strings wouldn't be enough.
Except that nowadays everyone slaps # frozen_string_literals: true on top of the file and all 1000 hashes will use the exact same object for the string key.
Also, not all Strings are mutable. It depends on whether they are frozen or not. But Strings are not Symbols.
Symbols are simply more efficient small identifiers. That's about it. They are nowhere near as powerful and useful as String objects, but they are (for most use cases) much more efficient than Strings.
if they were immutable and only initialized once I don't see the need for symbols
Well, they should still be costlier than a String object right? Even if both would not mutate. But this also carries the wrong thought process, because you try to singularize this on "both are equel". But they are not, not even when both are immutable.
You just wouldnt need them if strings were immutable. You dont write Ruby in cases where the performance/memory cost of a symbol vs string (if they were both immutable) matters.
Note - I am recommending people to never use HashWithIndifferentAccess. It is wrong to use it.
But I also understand why it is used. People don't want to care about whether they have a Symbol or a String as key.
The way how I approach Symbols is simple. Symbols are simple identifier. If I can, I use a Symbol rather than a String object. In most cases using symbols is also more efficient than using a String (there are some orthogonal results, in particular when it comes to garbage collection, but by and large Symbols are more efficient). They are also shorter to type: :name versus 'name'.
Symbols aren’t interchangeable though.
user = { name: "Alice", age: 30 }
puts user[:name] # Alice
puts user["name"] # nil
Well, the last is a String; and that String is not part of the Hash, so of course that is logical. Symbols are not Strings. Strings are not Symbols.
One can convert the two via .to_s or to_sym if one has to. This is another reason why HashWithIndifferentAccess is totally pointless. It is also no surprise this came from rails - rails is not really using ruby. I know this sounds weird, because of course rails is implemented in ruby primarily, but rails is IMO outside of the ruby world really. It took me a while to understand this. (It is much more clear with the recent behaviour of DHH, the meta-influence of shopify and so forth).
rails is not really using ruby. I know this sounds weird, because of course rails is implemented in ruby primarily, but rails is IMO outside of the ruby world really. It took me a while to understand this
Could you tell me more about this? Not the DHH bit, I've followed that, but about Rails not really being Ruby. I know rails is just a framework and I want to understand how to wrap my head around Ruby first before I do so around Rails.
FIREstopdropandsave@reddit
It still feels like symbols are a hack due to mutability of Ruby strings, if they were immutable and only initialized once I don't see the need for symbols...
syklemil@reddit
I took it a bit differently. By the time we get to
I more get the impression that they're useful for slapping together ad-hoc, anonymous structs/dataclasses. Essentially it winds up being something similar to tuples, just with names for access rather than numbers.
FIREstopdropandsave@reddit
Bro right in that section they admit it's because of string mutability/initialization
syklemil@reddit
How is that about mutability? Seems to me you'd rather need reference capabilities and/or string interning to avoid spawning as many strings as hashmaps
FIREstopdropandsave@reddit
Because they're mutable means they cant re-use the "name" string and are forced to initialize 1000 of them...
light24bulbs@reddit
That has nothing to do with mutability.
FIREstopdropandsave@reddit
Please explain.
If they are mutable you certainly cannot re-use them since they all have to have separate slots in memory.
syklemil@reddit
Even in the case with a language with immutable strings, it's entirely possible to initialize 1000 identical strings in a loop. If you want to avoid that, you need string interning or references.
The mutability doesn't help for use as dict keys, but as far as I can tell, just having immutable strings wouldn't be enough.
FIREstopdropandsave@reddit
Sure, but having mutable strings means you definitely cannot avoid the 1000 initializations
syklemil@reddit
Alright. But I still find that Ruby here seems to have some midway point between what in Python would be
and
that enables something similar to
only with
:nameand:ageas accessors instead of0and1.Which is kind of … I'm not sure if I see any need for it, but it seems kinda neat.
h0rst_@reddit
Except that nowadays everyone slaps
# frozen_string_literals: trueon top of the file and all 1000 hashes will use the exact same object for the string key.FIREstopdropandsave@reddit
Agreed, but i'm saying if from day 1 immutability was a thing there's a good chance symbols never get introduced into the language.
shevy-java@reddit
They are not a Hack.
Also, not all Strings are mutable. It depends on whether they are frozen or not. But Strings are not Symbols.
Symbols are simply more efficient small identifiers. That's about it. They are nowhere near as powerful and useful as String objects, but they are (for most use cases) much more efficient than Strings.
Well, they should still be costlier than a String object right? Even if both would not mutate. But this also carries the wrong thought process, because you try to singularize this on "both are equel". But they are not, not even when both are immutable.
FIREstopdropandsave@reddit
You just wouldnt need them if strings were immutable. You dont write Ruby in cases where the performance/memory cost of a symbol vs string (if they were both immutable) matters.
shevy-java@reddit
I am not sure:
https://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html
Note - I am recommending people to never use HashWithIndifferentAccess. It is wrong to use it.
But I also understand why it is used. People don't want to care about whether they have a Symbol or a String as key.
The way how I approach Symbols is simple. Symbols are simple identifier. If I can, I use a Symbol rather than a String object. In most cases using symbols is also more efficient than using a String (there are some orthogonal results, in particular when it comes to garbage collection, but by and large Symbols are more efficient). They are also shorter to type: :name versus 'name'.
Well, the last is a String; and that String is not part of the Hash, so of course that is logical. Symbols are not Strings. Strings are not Symbols.
One can convert the two via .to_s or to_sym if one has to. This is another reason why HashWithIndifferentAccess is totally pointless. It is also no surprise this came from rails - rails is not really using ruby. I know this sounds weird, because of course rails is implemented in ruby primarily, but rails is IMO outside of the ruby world really. It took me a while to understand this. (It is much more clear with the recent behaviour of DHH, the meta-influence of shopify and so forth).
iamstonecharioteer@reddit (OP)
Could you tell me more about this? Not the DHH bit, I've followed that, but about Rails not really being Ruby. I know rails is just a framework and I want to understand how to wrap my head around Ruby first before I do so around Rails.