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

后端 未结 10 1322
说谎
说谎 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条回答
  •  星月不相逢
    2020-11-28 07:24

    symbol is immutable and string is mutable.

    when we perform any operation on string then it create a new object and take memory. As we perform more and more operation on string means we are consuming more and more memory.

    symbol is object that are immutable mean if we perform any operation then it performs changes in original object, It will not create any object, that's why it is more profitable.

    for more info, you can click here

提交回复
热议问题