Ruby learning 2

Methods and so on 2

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.

Regular expressions

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.


/ = 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.

Method argument specification

First, specify the formal and actual arguments that you often see

def jojo(name, stand)
  p "#{name}Stand:#{stand}"

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.

Setting the default value

def jojo(name, stand = "None") #Set the default value for the second argument
  p "#{name}Stand:#{stand}"


=> "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}"

jojo("Jonathan", "task")

=> "Jonathan's Stand: Task"

Keyword arguments

def jojo(name:, stand: "None") #Set arguments like symbols
  p "#{name}Stand:#{stand}"

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"

Use your favorite arguments for keyword arguments

Any keyword and value can be passed as a hash type.

def jojo(name:, stand: "None", **z)
  p "#{name}Stand:#{stand}"
  p z

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"}


Recommended Posts

Ruby learning 4
Ruby learning 5
Ruby learning 3
Ruby learning 2
Ruby learning 6
Ruby learning 1
Ruby Learning # 25 Comments
Ruby Learning # 13 Arrays
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Ruby Learning # 14 Hashes
Ruby Learning # 33 Inheritance
Ruby Learning # 15 Methods
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Ruby Learning # 29 Classes & Objects
Ruby Learning # 20 Case Expressions
Ruby Learning # 24 Exponent Method
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Ruby Learning # 27 Writing Files
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
Ruby Learning # 2 Drawing a Shape
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby Learning # 18 If Statements (Con't)
Ruby on Rails basic learning ①
Ruby Learning # 11 Building a Calculator
Ruby Learning # 99 Personal Programming Notebook
Ruby on rails learning record-2020.10.07 ②
Ruby Learning # 10 Getting User Input
Ruby Learning # 8 Working With String
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Java learning (0)
Ruby Learning # 19 Building a Better Calculator
Completed Progate Ruby Learning Course III
Ruby basics
Ruby basics
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Ruby Review 2
Ruby addition
Refactoring Ruby
Completed Progate Ruby Learning Course II
Servlet learning
Ruby Learning # 22 Building a Guessing Game
Ruby setting 2
Ruby problem ⑦
Maven learning
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby settings 1