views:

181

answers:

2

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".

+3  A: 

The first example is syntactic sugar, normally you see this written like:

x = "Here is a string that " \
    "I want to split across " \
    "several lines..."

So this happens first, before to_sym. In the second example you are literally calling:

x = "hello".+( "world".to_sym )

Which is obviously not going to work since String#+ can't do anything useful with a symbol as an argument.

In short, don't do the first one, if you want to do "hello world".to_sym and you can't just write it like that for whatever reason then just use parenthesis: ("hello" + " world").to_sym

thenduks
+1  A: 

Two or more string literals put right beside each other like that are immediately treated as a single string literal. When the Ruby interpreter converts your code to tokens, it converts

"hello" " world"

to the single token string "hello world" and it converts

"hello" + " world"

to three tokens: string "hello", method +, and string " world". It would then concatenate the strings together later on when actually executing the code.

yjerem