[Ruby] "Reference to object" and "Contents of variable"

Introduction

If you're using Ruby, you'll probably come across the concept of "references to objects".

For example, if you have the following code:

sample.rb


#Define a method that destructively converts the passed string to uppercase
def replace!(str)
  str.upcase!
end

str = 'sample'
#Apply the replace method to the variable str
replace!(str) #=> "SAMPLE"

At this time, what was passed to the argument str of the replace method is "a reference to the String object'sample' stored in the variable str" . I think that such an explanation is often given.

But even if you hear this explanation

-** "What exactly does a reference to an object mean?" ** -** "What does it mean to refer to an object stored in a variable?" **

I think there are many people who think that

This time for such people

-** Reference to object ** -** Variable identity ** -** Passing by reference **

I will explain such things in an easy-to-understand manner.

Please take a look to the end.

Target audience

--People who started learning Ruby --People who have just started programming --People who don't understand "reference to object"

Contents of this article

1. What is a reference to an object? 2. What the variable is 3. What is passed to the method argument 4. Summary 5. Finally 6. Reference site

1. What is a reference to an object?

A "reference to an object" simply means ** "accessing the location of an object" **.

First of all, since Ruby is a object-oriented language , ** arrays and strings, as well as numbers and nil, are treated as objects. ** **

And in such an object-oriented language, when you define an object, at the same time, a memory area for storing the object is defined.

Simply put, it's like ** each object has an address. ** **

#Of the String object'Hello'Generate a
'Hello'

image.png

And ** accessing the address of the object defined in this way is called "reference to the object" . ** **

Roughly speaking, it is "reference to object = whereabouts of object" .

2. What the variable is

Next, let's consider the relationship between the memory area and variables that came out earlier.

For example, consider the following situation.

#In variable a'test'Substitute
a = 'test'

At this time, the following things are happening behind the scenes.

** ① String object is created (contents are'test') ** ** ② The address (memory area) is stored in the variable a **

image.png

In other words, the substance of a variable is "memory area" , and the value contained in that variable is actually ** "object stored in the variable (here'test'". ) 's address ”**.

Make sure that the variable a does not contain the string object itself.

3. What is passed to the method arguments

Now let's consider the situation where we pass this variable as a method argument.

Let's consider the example given at the very beginning.

sample.rb


#Define a method that destructively converts the passed string to uppercase
def replace!(str)
  str.upcase!
end

#String in variable str'sample'Substitute
str = 'sample'

#Apply the replace method to the variable str
replace!(str) #=> "SAMPLE"

What is happening at this time?

First of all, the "replace" method is defined, but ** The argument "str" ​​contained in this method is also a variable, so naturally an area is allocated in memory as mentioned earlier. ** ** (This area is not a fixed location, it is reserved when the method is called, and is released when the method finishes executing. **)

And when the method is defined ** this argument is tentative, so (dumb argument) , and there is nothing in it yet. ** ** Here, it is just "only the area is secured in the memory".

Then, as before, the ** string "sample" and the variable str are set. ** **

The summary up to this point is as follows.

image.png

Next, when you call the "replace! Method", the state immediately after that is shown in the figure below.

image.png

The point here is

-** The value of the variable (actual argument) str is a reference to the string object, which is copied as is to the argument (dummy argument) str **

about it.

And, in this way, passing "reference to an object stored in a variable" as an argument is called ** "passing by value of reference" **.

When this reference is passed by value, of course, the ** variable str and the argument str share the same object. ** **

Therefore, if the value of the argument str is changed, the contents of the object that lives at the address of the argument value will also be changed. ** ** (In technical terms, ** the object referenced by the argument str is changed **)

In this case, when "str.upcase!", Which is the content of "replace! Method", is executed, it will be as follows.

image.png

If you look at this, you can see that the value of the argument str hasn't changed, but the contents of the object pointed to by that argument have changed.

4. Summary

--A reference to an object is ** "Accessing the location of an object" ** --The substance of the variable is ** "Reference to the object stored in the variable" ** --By value of reference is ** "A reference to an object stored in a variable is passed as an argument" **

5. Finally

I hope the content of this article will be helpful to you.

Thank you for watching until the end.

6. References

Qiita article: [Ruby] Method arguments are passed by value? Pass by reference? Qiita article: Ruby destructive methods and passing references by value Rubyist Magazine: Understand the difference between passing by value and passing by reference

Recommended Posts