Using Ruby Symbols

前端 未结 5 991
无人及你
无人及你 2020-12-02 23:56

First time I tried learning Ruby was 2 years ago, now I have started again. The reason I stopped was because I could not understand the Symbol class. And now I am at the sam

5条回答
  •  执念已碎
    2020-12-03 00:39

    The O'Reilly Ruby Cookbook (p. 15) quotes Jim Weirich as saying:

    • If the contents (the sequence of characters) of the object are important, use a string.
    • If the identity of the object is important, use a symbol.

    Symbols are generally used as hash keys, because it's the identity of the key that's important. Symbols are also required when passing messages using certain methods like Object#send.

提交回复
热议问题