Pass arguments to the method and receive the result of the operation as a return value

I don't understand the arguments

I want to output "Mr.Yuki" ...

1 def rename(name)
2  name = "Mr.#{name}"
3 end
4
5 name = "Yuki"
6 rename(name)
7 puts name
Yuki

In this syntax, the output will be "Yuki" without Mr..

1 def rname (name) # Variable name received by rename method name 2 name = "Mr. # {name}" # Variable name received by rename method name 3 end

 A copy of the value passed as an argument is assigned to the variable of the method.

>~~~
 5 name = "Yuki" #This name is all the argument name
6 rename(name)
7 puts name
>~~~

 In short, the argument name and the variable name have the same name but are different.

 Explanation step by step.

 --Fifth line: The character string "Yuki" is assigned to the argument `name`.
 --Line 6: Call method rename with this `name` as an argument
 At this time, the value contained in `name` is copied.
 --First line: The copied "Yuki" is assigned to the variable `name` of the method rename.
 Therefore, the argument `name` and the variable` name` used in the method have the same value, and the value can be passed to the method.
 --Second line: However, changing the value of the variable `name` does not affect the argument` name` because the argument `name` is different.

## So how do you get the results you expect?
 Detailed explanation.

>~~~
1 def rename(name)
2   name = "Mr.#{name}"
3 end
>~~~
 --Second line: In the method `rename`, the character string with Mr. added to the` name` received from the argument is returned as the return value to the method caller.

>~~~
6 name = rename(name)
>~~~
 --Before the modification, you just called `rename (name)` and the method `rename`, but this time you are assigning` name = rename (name) `and the return value from the method to` name`.


1 def rename(name) 2 name = "Mr. # {name}" # ③ In response to the result of the 5th line, "Mr.Yuki" is assigned to name on the left side 3 end 4 5 name = "Yuki" # ① Read the formula with one "=" from the right side. Assign "Yuki" to variable name 6 name = rename (name) # ② rename is the method on the first line. Assign the answer of this method and the value of the argument (name) (* defined in the 5th line) to the variable name on the left side. 7 puts name # ④ The return value is output by puts

Mr.Yuki

 Now the value of `name` becomes a string with Mr. added by the method` rename`.

## at the end
 It's really hard to understand here.
 A lot of names come out. I couldn't understand it in my head.
 I numbered the processes and showed them in order, and some came in.
 It seems that a review is needed here.





Recommended Posts

Pass arguments to the method and receive the result of the operation as a return value
A way for Java / Scala developers to advance their careers as data engineers.
Pass arguments to the method and receive the result of the operation as a return value
Item 47: Prefer Collection to Stream as a return type
Takes out the HashMap value and returns the result sorted by the value of value as a list.
I want to pass the argument of Annotation and the argument of the calling method to aspect
Method to describe by dividing into multiple methods How to pass arguments How to use return value Method overload Pass by value and pass by reference
Display the average value of the evaluation as a star
[ruby] How to assign a value to a hash by referring to the value and key of another hash
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
[Ruby] Questions and verification about the number of method arguments
I want to call a method and count the number
How to specify an array in the return value / argument of the method in the CORBA IDL file
I tried to express the result of before and after of Date class with a number line
[Rails] Talk about paying attention to the return value of where
Method to add the number of years and get the end of the month
[Rails] Read the RSS of the site and return the contents to the front
[Swift5] How to communicate from ViewController to Model and pass a value
[Java] You might be happy if the return value of a method that returns null is Optional <>
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
[Ruby] How to use is_a? Differences from kind_of? and instance_of ?. How to check the type and return a boolean value.
Declare a method that has a Java return value with the return value data type
How to return a value from Model to Controller using the [Swift5] protocol
The story of forgetting to close a file in Java and failing
[Ruby] What happens if the method self is used as the return value?
The content of the return value of executeBatch is different between 11g and 12c
[Ruby] I want to extract only the value of the hash and only the key
How to change the value of a variable at a breakpoint in intelliJ
How to pass the value to another screen
Use the where method to narrow down by referring to the value of another model.
If it is Ruby, it is efficient to make it a method and stock the processing.
Find a value that is convenient to have a method and make it a ValueObject
How to convert a value of a different type and assign it to another variable
Create a Yellowfin custom formatter and display the minus of the number as △ (triangle)
I want to recursively get the superclass and interface of a certain class
Be absolutely careful when putting the result of and / or in a variable!
[JDBC ③] I tried to input from the main method using placeholders and arguments.
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
Make a margin to the left of the TextField
Set the time of LocalDateTime to a specific time
Output of how to use the slice method
pass two arguments to the URI with link_to
About call timing and arguments of addToBackStack method
How to display the result of form input
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
Arguments with default values Take the full_title method of the Rails tutorial as an example
[Jackson] A story about converting the return value of BigDecimal type with a custom serializer.
A memo about the types of Java O/R mappers and how to select them
When inserting from Java to MySQL, get Auto_Increment_ID (automatic numbering value) as the return value
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
Sample code to assign a value in a property file to a field of the expected type
[Java] The problem that true was returned as a result of comparing Integer with ==
A method that applies transforms in parallel to all elements and returns an array of return values that maintain their order
How to run the SpringBoot app as a service
I want to call a method of another class
How to return to the previous screen by Swipe operation
Return the execution result of Service class in ServiceResponse class
Item 47: Prefer Collection to Stream as a return type
Convert the array of errors.full_messages to characters and output
Bind the request to any class and receive it
[Java] How to get the maximum value of HashMap
I want to return the scroll position of UITableView!