I was setting up symbols in my code like:
"name_of_symbol".to_sym
However, my principal engineer picked it up during code review as a bad practice and asked me to set symbols like:
:"name_of_symbol"
When I asked him why? He said it's bad practice, but when I asked for what reason he just said it is, which is not really a satisfying answer. So how is it? Is there any difference at all?
Other answers argue correctly that :"foo bar"
is superior to "foo bar".to_sym
because it is clearer, better expresses your intention, is more readable, faster etc. But there is one more reason: "foo bar".to_sym
relies on String#to_sym
method, and there is a possibility (although remote) that this method might be redefined. This is the one non-cosmetic reason why :"foo bar"
is in principle better practice.