[Super Introduction] About Symbols in Ruby

It's been a month since I started learning with rails and Ruby. There are still many things I don't understand, so I'll post it as a record this time as well.

By the way, today, I used the symbol of "Introduction to Ruby to Become a Professional". A little while creating a unit conversion program? I summarized the symbols that became!

About symbols and how to use them

**-Symbols may be used instead of strings, but they are not necessarily the same objects as strings ** ** ・ The same symbol is the same object **

Is it roughly like this?

Also, as a way of defining,

: Symbol name : example => Can also be expressed as "example"

The big difference is ** Symbols are internally processed as integers and are immutable objects. Destructive changes do not work. ** **

Use with hash key

Now that the basics have been reorganized, the next step is how to use them in hashes.

 examples = { :a => "aaa", :b => "bbb", :c => "ccc"}

examples = {a: "aaa", b: "bbb", c: "ccc"} # Omitted form

call example [: a] => # "aaa"

All can be changed to symbols

 examples = { :a => :aaa, :b => :bbb, :c => :ccc}

Symbols are suitable for hash keys, so they are usually expressed as "symbol: value". (Due to immutable properties, processing speed, etc.)

Use in keyword arguments

Finally, regarding the use of keyword arguments. Basically, there is no problem with "symbol: value" as above.

      def example_length(length, from: :m, to: :m)
       units =  { m: 1.0, ft: 3.28, in: 39.37}

(length / units [from] * units [to]). round (2) # => units The to key is called like the value of the From key (m :) in the hash. end

As mentioned above, although it will be a mental point at the beginning of this book, it is the scariest thing to know, so In the future, I would like to keep moving my hands and organize the places I get caught in this way.

Recommended Posts

[Super Introduction] About Symbols in Ruby
About Ruby symbols
About Ruby Symbols
About eval in Ruby
About regular expressions in Ruby
Summary of hashes and symbols in Ruby
Introduction to Ruby 2
Ruby Learning # 1 Introduction
About ruby ​​form
About Ruby Hashes
About MacinCloud introduction
Class in Ruby
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
Heavy in Ruby! ??
About Ruby variables
About Ruby methods
Ractor super introduction
About regular expressions used in ruby sub method
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 the difference between classes and instances in Ruby
About Ruby if statement
About Ruby instance methods
About Java Polymorphism super ()
Output triangle in Ruby
Groovy super easy introduction
[Ruby] About instance generation
About redirect_to in form_with
Variable type in ruby
About the [ruby] operator
Fast popcount in Ruby
Thinking about logic Ruby
About docker.credentials.errors.StoreErrorTraceback in wsl2
About the difference between "(double quotation)" and "single quotation" in Ruby
Introduction to Rakefile that can be done in about 10 minutes
Introduction to Parallel Processing + New Parallel Execution Unit in Ruby Ractor
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Read design patterns in Ruby
About validation methods in JUnit
Write class inheritance in Ruby
Update Ruby in Unicorn environment
[Ruby] Exception handling in functions
Use ruby variables in javascript.
[For super beginners] DBUnit super introduction
[Ruby on Rails] about has_secure_password
What you learned about symbols
Multiplication in a Ruby array
[For super beginners] Ant super introduction
[Ruby] Introduction to Ruby Error statement
Ruby About various iterative processes
Birthday attack calculation in Ruby
[Super easy] Ruby environment construction