[Ruby] About symbols and destructiveness Ruby

4 minute read

How to write # ruby This time, we will symbolize the symbols and destructive methods.

About symbols

It looks like there are too many symbols and how to write them, and I’m not sure what they are. The colons were in front and behind, and they all looked like symbols…there was no sense of unity. The code I read was sporadic in its various ways of writing, and I didn’t understand any of its merits.

But it wasn’t. There were many ways to write a hash, not many ways to write a symbol.

Symbols were only written with a colon before the name

:key

The above is the symbol.

What confused me is that there are 4 patterns of writing as shown below, and it is mixed with the writing that looks like a symbol, so I misunderstood this as a symbol, which led to slow understanding. Oops.

It seems a little strange that you can 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 = {
  "Jutaro Kujo" => "Star Platinum",
  "Noriaki Kakyoin" => "Hierophant Green",
}

# Hash with symbol defined as 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 = {
  "Sorajo Tarojo": "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 "The Avdol stand is #{nomal_hash["Avdol"]} (string key)"
puts "Jotaro Kujo's stand is #{nomal_hash_two["Jotaro Kujo"]} (string key)"

# Hash made with symbol uses symbol key
puts "The stand of Avdol is #{symbol_hash[:Avdol]} (symbol key)"
puts "Kojiro Kujo's stand is #{symbol_hash_two[:Kyotaro Kujo]} (symbol key)"

# Hash written in Jason style also uses symbols as keys
puts "Igy's stand is #{json_like_hash[:Igy]} (symbol key)"
puts "The stand of Noriaki Kakyoin is #{json_like_hash_two[:Noriaki Kakyoin]} (symbol key)"

# Hashes that don't need quotations and keys are also symbols
puts "Igy stands at #{no_double_quote_hash[:Igy]} points (symbol key)"
puts "The stand of Noriaki Kakyoin is #{no_double_quote_hash_two[:Noriaki Kakyoin]} (symbol key)"

Output result

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

Destructive method

References [Ruby] passing by reference and destructive methods

It’s a story of passing by value, passing by reference, and passing by reference. It is the same as the pointer of C language.

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

Passing by reference means passing the location of the variable where the data (object) is, strictly speaking, the address of the memory reserved for the variable. In other words, in the case of x = 200, it can be said that the information that the data (object) of 200 is in the xxxx address of the variable of x is passed.

So what is passing by reference by value…

Pass the location of newly created data (object)

It should be possible…

That’s where the destructive method comes out

destroy_method.rb


# Substitution
p story = "bakemonogatari"

# Assign assignment
x = story

# Pass to destructive method
p story.upcase!

# All become the same
p story
p x

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

# Substitution
p story_two = "tukimonogatari"

# Assign assignment
y = story_two

# Pass to non-destructive methods
p story_two.upcase

Not affected by #upcase method
p story_two
p y

Output result

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

!! Is a destructive method. Destructive means having the ability to edit an object. Since we edited and shown the location of the newly created object, both story and x refer to the location of the value of the newly created object, so we puts the uppercased string.

On the other hand, the non-destructive method does not pass the reference part of the new object even if the object is newly created, so it does not seem to be affected by the upcase method.

object_id.rb


# Substitution
p story = "bakemonogatari"

# Assign assignment
x = story

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

# All become the same
p story.object_id
p x.object_id

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

# Substitution
p story_two = "tukimonogatari"

# Assign assignment
y = story_two

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

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

Output result

# All same ID
"bakemonogatari"
70291624072300
70291624072300
70291624072300
"##################################"
# Only the recreated object has a different object ID
"tukimonogatari"
70291624071720
70291624071800
70291624071800

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

Summary

It’s confusing, but I think that it has finally been verbalized. Please point out if you are wrong.