Why are symbols not frozen strings?

前端 未结 6 1923
广开言路
广开言路 2021-01-31 17:59

I understand the theoretical difference between Strings and Symbols. I understand that Symbols are meant to represent a concept or a name or an identifier or a label or a key, a

6条回答
  •  借酒劲吻你
    2021-01-31 18:24

    Another consideration is that "apple".each_char makes sense, but :apple.each_char doesn't. A string is an "ordered list of characters", but a symbol is a atomic datapoint with no explicit value.

    I'd say that HashWithIndifferentAccess actually demonstrates that Ruby symbols are fulfilling two different roles; symbols (which are essentially like enums in other languages) and interned strings (which are essentially a preemptive optimisation, compensating for the fact that ruby is interpreted so doesn't have the benefits of an intelligent optimising compiler.)

提交回复
热议问题