Currently, I am studying to get the Ruby engineer certification exam silver. I still have a lot of understanding of the language, so I will output from the basics.
Why use it, what is a regular expression in the first place? I will omit the story, and this time I will decompose the following regular expression that appeared in the mock problem.
/^[hc].*o$/i
/ = Condition surrounded by slashes ^ = Meaning of the beginning of a line. In relation to $ and the set, I think it means "Please satisfy the conditions of the enclosed range from the beginning of the line to the end of the line." Without this, even if a character string that does not meet the conditions is confused, it will pass. $ = End of line. Almost the same as above. In rails, it cannot be used after rails4, and \ A and \ z are used. You can use characters in the range [hc] = []. In this case h or c. * However, h, H, c, C can be used due to the existence of i described later. . * = 0 or more characters of some kind. In short, anything is fine, and there is no need for letters. o = alphabet o. * O or O due to the influence of i described later. i = Case insensitive.
First, specify the formal and actual arguments that you often see
def jojo(name, stand)
p "#{name}Stand:#{stand}"
end
jojo("Jotaro", "Star Platinum")
=> "Jotaro's Stand: Star Platinum"
Since there are two formal arguments, an error will be thrown if the number of actual arguments is insufficient.
def jojo(name, stand = "None") #Set the default value for the second argument
p "#{name}Stand:#{stand}"
end
jojo("Jonathan")
=> "Jonathan's Stand: None" #The value is returned even if there is no second argument
It is also possible to change from the default value.
def jojo(name, stand = "None")
p "#{name}Stand:#{stand}"
end
jojo("Jonathan", "task")
=> "Jonathan's Stand: Task"
def jojo(name:, stand: "None") #Set arguments like symbols
p "#{name}Stand:#{stand}"
end
jojo(stand: "Killer queen", name: "Kira Yoshikage") #Actual arguments are also passed by specifying keywords. Since the keyword is specified, there is no problem even if the order of the arguments is changed.
=> "Kira Yoshikage Stand: Killer Queen"
Any keyword and value can be passed as a hash type.
def jojo(name:, stand: "None", **z)
p "#{name}Stand:#{stand}"
p z
end
jojo(name: "Diabolo",stand: "King Crimson", dododo: "Beside me", gogogo: "Don't get closer")
#Stores keys and values that are not specified as keywords in the formal argument z
=> "Diavolo Stand: King Crimson"
{:dododo=>"Beside me", :gogogo=>"Don't get closer"}
To_Be_Continued...
Recommended Posts