This is a personal memo.
About the meaning of the description that the variable with a colon after it is specified in the argument of the method.
** ▼ This kind of **
def jadge(x:)
python
def jadge(x:)
p "exist x" if x.present?
end
The colon at the end of the argument specifies ** a variable name **. (Called ** keyword argument **)
Normally, the arguments are assigned to the first argument, the second argument ,,, in the order in which they are passed, but if you add a colon at the end, you can pass the value of the argument that matches the variable name.
Especially when there are multiple variables to handle, it is convenient because you can specify them by variable name.
Specify the variable name with a colon
#Function definition
def jadge(x:, y:)
p "exist x" if x.present?
p "exist y" if y.present?
end
#Function execution
jadge(y: false, x: true)
=> "exist x"
python
def jadge(x, y)
p "exist x" if x.present?
p "exist y" if y.present?
end
y = false
x = true
jadge(y, x)
=> "exist y"
The values are entered in the order in which they were passed, not the variable names.
Recommended Posts