'Ruby: what's the difference between initializing a string using quotes vs colon?
Whats' the difference between initializing a string using quotes vs preceding it with the colon? I.e "bobo" vs :bobo. When you inspect them they appear the same but when you compare them the result evaluates to false.
irb(main):006:0> r = "bobo"
=> "bobo"
irb(main):007:0> puts r
bobo
=> nil
irb(main):008:0> t = :bobo
=> :bobo
irb(main):009:0> puts t
bobo
=> nil
irb(main):010:0> puts r == t
false
=> nil
irb(main):011:0> s = "bobo"
=> "bobo"
irb(main):012:0> puts r == s
true
=> nil
Solution 1:[1]
Notice the following:
"foo".object_id
=> 70353007334300
"foo".object_id
=> 70353007354360
:foo.object_id
=> 413928
:foo.object_id
=> 413928
Symbols are cached (same object_id) whereas strings are instantiated every time. Keep this in mind for performance knowledge but also keep in mind garbage collector ;)
Solution 2:[2]
The String-like thing beginning with a colon is a Symbol:
:bobo.class
# => Symbol
"bobo".class
# => String
When you inspect them, they look different:
:bobo.inspect
# => ":bobo"
"bobo".inspect
# => "\"bobo\""
When you print them with puts they look the same, because puts calls to_s on its arguments, and :bobo.to_s returns "bobo":
:bobo.to_s
# => "bobo"
:bobo.to_s == "bobo"
# => true
If you'd like to understand better the differences and how Symbols are used, perusing the documentation linked to above is a good place to start, as is any Ruby tutorial. This article is also worth reading: The Difference Between Ruby Symbols and Strings.
Solution 3:[3]
The "string with a colon" as you say, is a symbol. While they are similar to strings, symbols are immutable. Mutable objects can be changed after assignment. Immutable objects(:symbols) can not be changed after assignment. They can only be overwritten. Ruby is unique and is such a dynamic language, things can and often do change at runtime. Symbols are more rigid and won't be unexpectedly changed at runtime.
Solution 4:[4]
the main difference between symbol and string is memory allocation and mutability. strings can be altered anytime.
'prasad' << 'surase' => "prasadsurase"
:prasad << 'surase'
NoMethodError: undefined method `<<' for :prasad:Symbol
also two strings with same value(eg 'prasad' and 'prasad') have two different memory locations and have different object ids.
'prasad'.object_id => 102809450
'prasad'.object_id => 102894570
'prasad'.object_id => 103057580
whereas two same symbols are the same and have the same memory location since there is only a single object.
:prasad.object_id => 2358408
:prasad.object_id => 2358408
:prasad.object_id => 2358408
also, when string is created, it needs to be allocated memory n when not referred, it needs to be garbage collected but symbols stay in memory throughout the programs operation and are not garbage collected when not referred. this affects the performance of the program. u can collect all the declared symbols as
Symbol.all_symbols.include?(:prasad) => true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | prasad.surase |
