Differences between Ruby strings and symbols [Beginner]

This is the first post.

Please point out any mistakes.

Difference between strings and symbols

Strings and symbols look similar but different.

The difference is that the given object ID is different.

In the case of a character string, the object ID given will be different even if two identical character strings are prepared, but in the case of a symbol, the same object ID will be given even if two same character strings are prepared.

Looking specifically, it looks like this. Ruby_symbol1.png Execution result Ruby_symbol2.png If you display the object ID using the "object_id" of the method that displays the object ID for the object, it will look like this.

The object IDs for the top two strings ("JUN") are displayed differently, but the object IDs for the symbol (: JUN) are the same.

By the way Ruby_symbol3.png Ruby_symbol4.png Obviously, if you run it again, the object ID given to the string will be different compared to the first time, but the object ID of the symbol will be the same as the first time.

Furthermore, at the first execution, it was displayed in all uppercase letters (: JUN), but if it is written in lowercase letters (: jun), an object ID different from that in uppercase letters will be given.

Recommended Posts

Differences between Ruby strings and symbols [Beginner]
Differences between "beginner" Java and Kotlin
Symbols and Destructive Ruby
[Understanding] Differences between hashes and arrays in Ruby
Ruby: Differences between class methods and instance methods, class variables and instance variables
Differences between Ruby syntax error statements in Ruby and binary
About Ruby hashes and symbols
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
Differences in how to handle strings between Java and Perl
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Differences between Applet class and JApplet class
Differences between Java and .NET Framework
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
Note: Difference between Ruby "p" and "puts"
Difference between Ruby instance variable and local variable
Differences between preface and postfix of operators
[Java] Differences between instance variables and class variables
Summary of hashes and symbols in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Rails] Difference between redirect_to and render [Beginner]
About Ruby symbols
Differences between Spring Initializr packaging JAR and WAR
Summarize the differences between C # and Java writing
[Ruby] Difference between puts and return, output and return value
Ruby and Gem
Ruby How to convert between uppercase and lowercase
[Ruby] I thought about the difference between each_with_index and each.with_index
About the difference between classes and instances in Ruby
In fact, Ruby distinguishes between line breaks and whitespace.
Differences between browser sessions and cookies and Rails session and cookies methods
Differences between Fetch Type LAZY and EAGER in Hibernate
[Java beginner] Difference between length and length () ~ I don't know ~
[Ruby] Classes and instances
[Ruby] Big Decimal and DECIMAL
Ruby inheritance and delegation
Switch between JDK 7 and JDK 8
Difference between vh and%
Difference between i ++ and ++ i
[Java Bronze learning] Differences between encapsulation, data hiding, and information hiding
Differences between namespace, module, scope and as in Rails routing
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
Think about the differences between functions and methods (in Java)
The difference between programming with Ruby classes and programming without it