Conclusion: In Ruby, whether it is value transfer or reference transfer or ruby Transfer

Source: Internet
Author: User

Conclusion: In Ruby, whether it is value transfer or reference transfer or ruby Transfer

In ruby, everything is an object, and what you pass to the method is essentially an object-reference ). Variables in ruby are references to objects.

Let's take a look

Def pref2 (agr) agr. downcaseenda2 = 'pref2' PREF2 (a2) puts "# {a2}" # output pref2

Why does a2 still use the capital "pref2" after PREF2 is called? Check the downcase source code to find out the problem.

Link: https://ruby-doc.org/core-2.4.1/string.html, search for downcase

 

Str = rb_str_dup (str); this sentence is actually quite clear. In fact, dwoncase returns a new string, not a reference to the string object you passed in.
So after the pref2 method, the a2 value does not change.

 

Let's take a look.

Def pref1 (agr) agr. downcase! Enda1 = 'hello' pref1 (a1) puts "# {a1}" # output HELLO

Why does the value of pref1 Method a1 change. Let's take a look at downcase first! Source code

Link: https://ruby-doc.org/core-2.4.1/string.html, search for downcase!

 

After checking the source code, it is found that the reference of the passed object has not been changed from start to end. Therefore, the reference of a1 is returned, so the content of the a1 object is in dwoncase! Method is changed. Next let's continue.
Def pref3 (a, B) a, B = B, aenda, B = 1, 2pref3 (a, B) puts a, B # output 1, 2

Why did the result of exchanging a and B in pref3 remain unchanged? This problem is indeed difficult to understand. We can convert your example into a better understanding.

 

Change

Def pref3 (a) a = 5 enda = 1pref3 (a) puts a # output 1

Clearly passed a into and assigned it a value of 5. Why is it still 1? We can naturally think of whether a is not the same thing as a in the method. For verification, we further transformed our approach

 

Rebuild

Def pref3 (a) a = 5a. object_idenda = 1a. object_id # output 3pref3 (a) # output 11 puts

 

We found that the object_id of two a is different. They are not an object, but two.

Now we understand the key to the problem. The local variable a in the method points to object 5, while the external variable a still points to object 1.

 

The entire process can be understood:

 

Now let's look back at the position of a and B in your method, but it has no influence on the external definition of a and B. Because they basically save the reference of different objects.

Ruby transmits the reference copy instead of the reference itself. (Actually, it can be understood as passing values)
However, for basic types, the object itself is directly transmitted. For example:
Refer to this http://www.ruby-doc.org/core-1.9/classes/Fixnum.html
Write

 

"Fixnum objects have immediate value. this means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. assignment does not alias Fixnum objects. there is refreshing tively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum. ".

 

I think this article can end this topic: http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/
More interesting discussion process: http://www.khelll.com/blog/ruby/ruby-pass-by-value-or-by-reference/ collected from: http://www.iteye.com/topic/1117575 Author: Locke Liu Great God Thank you very much ~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.