Symbols and Destructive Ruby

How to write ruby

This time we will verbalize symbols and destructive methods.

About symbols

It looks like there are too many symbols and writing styles, so I'm not sure what it is. The colon was in front of or behind, and all of them were symbols ... it seemed that there was no sense of unity. This is because the code I read was written in various ways, and none of the merits was clear.

But it wasn't. Rather than having a lot of ways to write symbols, there were many ways to write hashes.

** The symbol was just a notation with a colon in front of the name **

:key

The above is the symbol.

I was confused because there are 4 patterns of writing as shown below, and the writing style that looks like a symbol is mixed, so misunderstanding this as a symbol led to slow understanding in the first place. Oops.

It feels a little strange to be able to access the value even in Japanese. The version of ruby is

ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

is.

various_hash.rb


#Ordinary hash
nomal_hash = {
  "Avdol" => "Magician's red",
  "Igy" => "the fool",
}

nomal_hash_two = {
  "Jotaro Kujo" => "Star Platinum",
  "Noriaki Kakyoin" => "Hierophant Green",
}

#A hash defined with a symbol as a key
symbol_hash = {
  :Avdol => "Magician's red",
  :Igy => "the fool",
}

symbol_hash_two = {
  :Jotaro Kujo=> "Star Platinum",
  :Noriaki Kakyoin=> "Hierophant Green",
}

#When the symbol is a key, it can be abbreviated like JSON
json_like_hash = {
  "Avdol": "Magician's red",
  "Igy": "the fool",
}

json_like_hash_two = {
  "Jotaro Kujo": "Star Platinum",
  "Noriaki Kakyoin": "Hierophant Green",
}

#No need for double quotes
#This is the same as JavaScript object notation
no_double_quote_hash = {
  Avdol: "Magician's red",
  Igy: "the fool",
}

no_double_quote_hash_two = {
Jotaro Kujo: "Star Platinum",
Noriaki Kakyoin: "Hierophant Green",
}

#How to access each
#Normal hashes use string keys
puts "Avdol's stand#{nomal_hash["Avdol"]}(String key)"
puts "Jotaro Kujo's stand#{nomal_hash_two["Jotaro Kujo"]}(String key)"

#Hashes made of symbols use symbol keys
puts "Avdol's stand#{symbol_hash[:Avdol]}(Symbol key)"
puts "Jotaro Kujo's stand#{symbol_hash_two[:Jotaro Kujo]}(Symbol key)"

#Jason-style hashes also use symbols as keys
puts "Igy's stand#{json_like_hash[:Igy]}(Symbol key)" 
puts "Noriaki Kakyoin's stand#{json_like_hash_two[:Noriaki Kakyoin]}(Symbol key)" 

#Hashes that do not require quotes are also symbolic
puts "Igy's stand#{no_double_quote_hash[:Igy]}It is a point (symbol key)"
puts "Noriaki Kakyoin's stand#{no_double_quote_hash_two[:Noriaki Kakyoin]}It is a point (symbol key)"

Output result

Avdol's stand is Magician's red (string key)
Jotaro Kujo's stand is Star Platinum (string key)
Avdol's stand is Magician's red (symbol key)
Jotaro Kujo's stand is Star Platinum (symbol key)
Igy's stand is the fool (symbol key)
Noriaki Kakyoin's stand is Hierophant Green (symbol key)
Igy's stand is the fool point (symbol key)
Noriaki Kakyoin's stand is a Hierophant Green dot (symbol key)

Destructive method

References [Ruby] Reference Passing and Destructive Methods

It's a story of passing by value, passing by reference, and passing by reference. It is familiar to the story of C language pointers.

Passing by value means sending the data (object) itself. If x = 200, it means that the data (object) of 200 itself is copied and passed.

Passing by reference means passing the ** variable location ** with the data (object), strictly speaking, the memory address ** reserved for the variable. In other words, in the example of x = 200, the information that the data (object) of 200 is at the xxxx address of the variable x is passed.

So what is the value passing of a reference ...

** Pass the location of newly created data (object) **

You should be able to say ...

That's where the destructive method comes up

destroy_method.rb


#Substitution
p story = "bakemonogatari"

#Substitution of assignment
x = story

#Pass to a destructive method
p story.upcase!

#All will be the same
p story
p x

p "##################################"

#Substitution
p story_two = "tukimonogatari"

#Substitution of assignment
y = story_two

#Pass to non-destructive method
p story_two.upcase

#Not affected by the upcase method
p story_two
p y

Output result

"bakemonogatari"
"BAKEMONOGATARI"
"BAKEMONOGATARI"
"BAKEMONOGATARI"
"##################################"
"tukimonogatari"
"TUKIMONOGATARI"
"tukimonogatari"
"tukimonogatari"

!! If you add, it becomes a destructive method. Destructive means having the ability to edit objects. Since I edited it to show the location of the newly created object, both story and x refer to the location of the value of the newly created object, so I puts the capitalized string.

On the contrary, the non-destructive method does not seem to be affected by the upcase method because it does not pass the reference part of the new object even if a new object is created.

object_id.rb


#Substitution
p story = "bakemonogatari"

#Substitution of assignment
x = story

#Pass to a destructive method
p story.upcase!.object_id

#All will be the same
p story.object_id
p x.object_id

p "##################################"

#Substitution
p story_two = "tukimonogatari"

#Substitution of assignment
y = story_two

#Pass to non-destructive method
p story_two.upcase.object_id

#Not affected by the upcase method
p story_two.object_id
p y.object_id

Output result

#All the same ID
"bakemonogatari"
70291624072300
70291624072300
70291624072300
"##################################"
#Object ID that is different only for the recreated one
"tukimonogatari"
70291624071720
70291624071800
70291624071800

There is a difference in object ID between destructive and non-destructive.

Summary

It's complicated, but I think I've finally made it into a language. Please point out if you make a mistake.

Recommended Posts

Symbols and Destructive Ruby
About Ruby hashes and symbols
About Ruby symbols
About Ruby Symbols
Ruby and Gem
Summary of hashes and symbols in Ruby
Differences between Ruby strings and symbols [Beginner]
[Ruby] Classes and instances
[Ruby] Big Decimal and DECIMAL
Ruby classes and instances
Ruby inheritance and delegation
Ruby syntax errors and countermeasures
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
About Ruby and object model
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby methods and classes (basic)
Creating Ruby classes and instances
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Ruby standard input and various methods
About Ruby single quotes and double quotes
[Ruby basics] split method and to_s method
About Ruby product operator (&) and sum operator (|)
Write keys and values in Ruby
[Super Introduction] About Symbols in Ruby
Ruby arrays, hashes, and symbols are confusing, so I summarized them.
Project ruby and rails version upgrade
About object-oriented inheritance and about yield Ruby
With ruby ● × Game and Othello (basic review)
Upgrade and switch Ruby versions on Windows 10
Note: Difference between Ruby "p" and "puts"
Make bubble sort and selection sort in Ruby
Difference between Ruby instance variable and local variable
[Ruby] Simplify each using map and inject
Explanation of Ruby Time and Date objects
[Ruby] Classification and usage of loops in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Easy] How to upgrade Ruby and bundler
Comparison of JavaScript objects and Ruby classes
Write code using Ruby classes and instances
[Ruby] Difference between print, puts and p
Convert JSON to TSV and TSV to JSON with Ruby