Why are empty Arrays and Hashes treated differently when cast to string and then to symbol?

ruby

Solution

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.

:[] == :"[]" #=> true

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.

Problem

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 => :"{}" ```

Original source