In Ruby, why are these two operations different for empty Arrays and Hashes?
Empty Array:
[].to_s.to_sym => :[]
Empty Hash:
{}.to_s.to_sym => :"{}"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They aren’t really different, it’s just that they’re displayed differently. The
{character can’t be the start of a symbol, so it’s enclosed in quote marks. It’s the same thing you’d do if you wanted to create a symbol that had a-in it, since otherwise it would be interpreted as the subtraction operator. In fact, you can go into IRB and test that the quotes don’t really affect the symbol.So, basically, one is able to use a shorter form, and the other has to be more verbose so that the parser can understand it. But there’s not an essential difference in the meaning or form of the two.