What's the difference between a string and a symbol in Ruby?

后端 未结 10 1327
说谎
说谎 2020-11-28 06:52

What\'s the difference between a string and a symbol in Ruby and when should I use one over the other?

10条回答
  •  旧时难觅i
    2020-11-28 07:39

    What are the differences between Symbols and Strings?

    1. Symbols are immutable: Their value remains constant.
    2. Multiple uses of the same symbol have the same object ID and are the same object compared to string which will be a different object with unique object ID, everytime.
    3. You can't call any of the String methods like split on Symbols.

    From Understanding Differences Between Symbols & Strings in Ruby

    If you know Chinese, you can also read 理解 Ruby Symbol.

提交回复
热议问题