Why are empty arrays and hashes handled differently when they are passed to a string and then to a character?

In Ruby, why are these two operations different for empty arrays and hashes?

An empty array:

[].to_s.to_sym => :[] 

Empty hash:

{}.to_s.to_sym => :"{}"
+3
source share
2 answers

They are not completely different, they just appear differently. A character {cannot be the beginning of a character, so it is enclosed in quotation marks. This is the same thing you would do if you wanted to create the character you were in -, because otherwise it would be interpreted as a subtraction operator. In fact, you can go to IRB and check that the quotation marks do not affect the character.

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

, , , , . .

+8

[] - "[]", {} - "{}". , :[] :"[]". , :"[]" , :"{}", Ruby .

+4

All Articles