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

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

    A symbol is something you use to represent names and strings. You would want to use a symbol when you may have need to use a string several times as this far easier and more productive.

    And just found this via google, which may offer greater detail: Here you go

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

    The statement:

    foo = "bar"
    

    creates a new object in memory. If we repeat the statement:

    foo = "bar"
    

    We create another object.

    To understand it more clearly please try this code in IRB:

    foo = "bar"
    puts "string #{foo} with object id = #{foo.object_id}"
    foo = "bar"
    puts "string #{foo} with object id = #{foo.object_id}"
    

    You will get output like:

    string bar with object id = 70358547221180
    string bar with object id = 70358548927060
    

    which clearly shows there are two different object for the same string. Now if you use a symbol it will create one object per symbol so:

    foo = :bar
    puts "symbol #{foo} with object id = #{foo.object_id}"
    foo = :bar
    puts "symbol #{foo} with object id = #{foo.object_id}"
    

    shows:

    symbol bar with object id = 7523228
    symbol bar with object id = 7523228
    

    which means there is only one object for :bar.

    Further, Symbols are immutable and you can't call any of the String methods like upcase or split on Symbols.

    Comparing Symbols are faster than comparing Strings. Symbols can be thought of as constant/immutable strings that form a unique set that are effectively converted to memory pointers on the heap. This means comparing two symbols is fast because you are just comparing two integers (memory pointers).

    Strings are mutable so the memory pointer to their value on the heap can change after modification. This means comparison operations are slower because duplicates can exist that are semantically equivalent.

    Use a symbol when you are sure that the value will remain constant, for example use symbols for hash keys. Use a string when you want to change the value or want to use a string method on it.

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

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

    Case where symbol can be disaster. Lets say you have params.map(&:to_sym) in your rails controller . Now here if you are converting the data provided by the user to symbol due to some reason then it could be dangerous. If the data provided by the user is too large and as we know that symbol is not a garbage collector, you might end up exhausting your server's memory which can takedown your website.

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