x = "hello" " world".to_sym puts x.class
This works and allows me to concatenate the two strings into a symbol, producing the output:
Symbol
But if I change it slightly to use a + instead of a space separating the hello and world strings, I get an error:
x = "hello" + " world".to_sym
puts x.class
This produces the following error - I think this is because it's trying to 'cast' the "world".to_sym call back to a string before concatenating it onto the "hello" string:
in `+': can't convert Symbol into String (TypeError)
I'm wondering what makes the first example work? Does anyone know what the order of operations is here? I suspect the two strings are actually treated as one, that the space between them is ignored and they are concatenated before the to_sym method is called. I actually would have expected it to work no differently than the second example because I thought the to_sym would be called on the "world" string BEFORE any attempt is made to join the contents of "hello" and "world".