0
0

More than 1 year has passed since last update.

個人的Rubyメソッドまとめ

Last updated at Posted at 2022-12-28

1、メソッドの定義から

defを使って定義。

def hello()
    puts "hello world"
end

定義されたメソッドは、それより下の位置から呼び出すことができる。

def hello()
    puts "hello world"
end

hello()

出力結果として

hello world

2、メソッドの命名規則

メソッドの名前は、次のルールに従う。
・1文字目:英語または、「_」(アンダーバー)
・2文字目以降:英語の大文字・小文字・数字「_」(アンダーバー)
・慣習として、メソッドの先頭には大文字を使わない

3,引数と戻り値を追加する

def sum(x, y)  # xとyが引数
    return x + y  # 戻り値(returnは省略可)
end

puts sum(3, 4)  
puts sum(300, 400)
num = sum(30, 40)  # numに戻り値が代入される
puts num   

出力結果は

7
700
70
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0