About Ruby Symbols

symbol

A symbol is an object that has a one-to-one correspondence with an arbitrary character string. The syntax of the symbol is that the symbol defines a colon (:) followed by any name.

: Symbol name

(Example)

:sushi 
:ruby
:pen 

Difference between symbols and strings

:sushi.class # =>Symbol
'sushi'.class # =>String

Symbols are objects of the Symbol class The string is an object of the String class

string = 'sushi'
string.upcase! # => "SUSHI"

#Symbols are immutable, so destructive changes are not possible
symbol = :sushi 
symbol.upcase! # => ruby.rb:5:in `<main>': undefined method `upcase!' for :sushi:Symbol (NoMethodError)

Strings can be destructively changed Symbol is impossible

Mutable means changeable, Immutable means immutable, immutable.

Destructive changes can be applied to mutable objects such as strings. But, Destructive changes cannot be applied to immutable objects such as symbols. Therefore, the symbol is suitable for the purpose of "I want to give a name to something, because it is a name, I don't want anyone to change it without permission".

◎ 4 types of immutable objects are data types

--Numeric value: integer / float class --Symbol: symbol class --Boolean: true, false class --nil: nil class

Symbol features / main uses

――It looks like a character string on the surface, so it is easy for programmers to understand. --Because it is an integer internally, the computer can compare values at high speed. --The same symbol is the same object, so memory usage is good. -Since it is immutable, there is no need to worry about changing the value without permission.

Symbols are often used when you want to be able to identify a name in your source code, but the name does not necessarily have to be a string. A typical use case is a hash key. You can use symbols as hash keys to retrieve values faster than strings.

Referenced literature

Introduction to Ruby for those who want to become a professional

Recommended Posts

About Ruby symbols
About Ruby Symbols
About Ruby hashes and symbols
About ruby ​​form
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
[Super Introduction] About Symbols in Ruby
About ruby block
About Ruby Hashes
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
Symbols and Destructive Ruby
About Ruby Hashes (continued)
[ruby] About here documents
About Ruby if statement
About Ruby instance methods
[Ruby] About instance generation
About the [ruby] operator
Thinking about logic Ruby
Explanation about Ruby Range object
[Ruby on Rails] about has_secure_password
What you learned about symbols
About regular expressions in Ruby
About =
About Ruby and object model
About Ruby classes and instances
Explanation about Ruby String object
About the behavior of ruby Hash # ==
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About method.invoke
Ruby learning 4
About Kotlin
About attr_accessor
[Ruby] Array
About Hinemos
Ruby basics
Ruby learning 5
About inheritance
Ruby basics
About params
About Docker
Ruby Review 2
Ruby addition
Refactoring Ruby
About Rails 6
About form_for
About Spring ③
[Ruby on Rails] About bundler (for beginners)
Ruby learning 3
About enum
About polymorphism
About hashes
About JitPack