The method can be defined by yourself as well as the one prepared in advance. A method is a set of fixed processes. Just write the method name in the program, and the process written between the def method name and end of the method will be executed. To be The definition part of the method is not read until the method is executed
def method name
#Process to be executed
#The character written next to def is the method name
end
Example:
#Method definition
def say_hello
puts "Hello World"
end
#Method execution
say_hello
The def ~ end part is skipped at first Call the method with say_hello, read the definition part for the first time, and execute the process in the definition
The value passed to the location called by the method The final value after the method has been processed Example:
def sample
"1"
"2"
"3" #The last line is the return value
end
puts sample # => 3
If you write return ◯◯ in a method, the expression that follows return will be the return value of that method. The return value is determined when return is used, and the method is forcibly terminated when the processing of that line is completed. In Ruby, the return statement can be omitted. If omitted, the return value of the method will be the value of the last executed expression in the method. Example:
def sample
"1"
"2"
"3"
return "4" #The process ends here
"5" #Not called
"6" #Not called
end
puts sample # => 4
Range in which defined variables can be used Variables that are basically defined outside the method cannot be used inside the method Conversely, variables defined inside a method cannot be used outside the method.
A value that can be passed to a method, etc. Arguments allow the method to handle the values of variables outside the scope (variables outside the scope) inside the method. There is a Cali argument that is described in () when defining a method and used for processing, and an actual argument that describes the value to be passed in () when calling a method. There is no difference in behavior whether the names of the actual argument and the formal argument are the same or different. Multiple arguments can be prepared, and they are called "first argument", "second argument" ... "○ th argument" in order from the left. The number of actual and formal arguments must always match
def method name(Formal argument)
#processing
end
#Method call
Method name(Actual argument)
Example:
def sample(number) #(2) Receive the value of the actual argument "3" with the formal argument number
puts number * number #③ "3" using the number to which "3" is substituted*3 ”and output
end
sample(3) #(1) Call the method with the numerical value "3" as the actual argument
The times, each method can be used with variables defined outside the method, without using any arguments. Variables defined in the method cannot be used like def
Example:
num = 1
lists = [1, 2, 3]
lists.each do |list|
num = num +list
end
puts num
#Output result
7
Example:
lists = [1, 2, 3]
lists.each do |list|
num = 1
num = num + list
end
puts num
#I get an error
Recommended Posts