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

后端 未结 10 1265
说谎
说谎 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:17

    There are two main differences between String and Symbol in Ruby.

    1. String is mutable and Symbol is not:

      • Because the String is mutable, it can be change in somewhere and can lead to the result is not correct.
      • Symbol is immutable.
    2. String is an Object so it needs memory allocation

      puts "abc".object_id # 70322858612020
      puts "abc".object_id # 70322846847380
      puts "abc".object_id # 70322846815460
      

      In the other hand, Symbol will return the same object:

      puts :abc.object_id # 1147868
      puts :abc.object_id # 1147868
      puts :abc.object_id # 1147868
      

    So the String will take more time to use and to compare than Symbol.

    Read "The Difference Between Ruby Symbols and Strings" for more information.

    0 讨论(0)
  • 2020-11-28 07:20

    Symbols and strings are completely different this post has a little insight into the differences. As to when and where to use them, there is a pretty extensive post on this subject over on has many :through.

    0 讨论(0)
  • 2020-11-28 07:23

    An additional difference between String and Symbol is that a String has a lot more methods on it for string manipulation, while a Symbol is a relatively lean object.

    Check out the documentation for the String class and the Symbol class.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-28 07:27

    Strings are Mutable , Symbols arre immutable
    Note:Mutable objects can be changed after assignment while immutable objects can only be overwritten http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/

    0 讨论(0)
  • 2020-11-28 07:30

    The main difference is that multiple symbols representing a single value are identical whereas this is not true with strings. For example:

    irb(main):007:0> :test.object_id
    => 83618
    irb(main):008:0> :test.object_id
    => 83618
    irb(main):009:0> :test.object_id
    => 83618
    

    Those are three references to the symbol :test, which are all the same object.

    irb(main):010:0> "test".object_id
    => -605770378
    irb(main):011:0> "test".object_id
    => -605779298
    irb(main):012:0> "test".object_id
    => -605784948
    

    Those are three references to the string "test", but are all different objects.

    This means that using symbols can potentially save a good bit of memory depending on the application. It is also faster to compare symbols for equality since they are the same object, comparing identical strings is much slower since the string values need to be compared instead of just the object ids.

    As far as when to use which, I usually use strings for almost everything except things like hash keys where I really want a unique identifier, not a string.

    0 讨论(0)
提交回复
热议问题