0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

メソッド の 定義 と 戻り値

Last updated at Posted at 2022-09-10

目的

  • メソッド の 使い方 を 理解する

ポイント

  • メソッド とは まとまった一連の処理 のこと
  • メソッドの最後に処理した値は呼び出し元に戻される

書き方の例

def buy_fruits

  apple = "apple"

  if apple == "apple"
    puts "りんごを渡す"
  elsif banana == "banana"
    puts "バナナを渡す"
  elsif lemon == "lemon"
    puts "レモンを渡す"
  end
end

buy_fruits

~実際の表示~
りんごを渡す

注意するポイント

  • 「puts」という命令の戻り値は nill となる

具体的な例

  • 上記の例に apple という 戻り値を返すようコードを追加する
require "pry"

def buy_fruits
  apple = "apple"

  if apple == "apple"
    puts "りんごを渡す"
  elsif banana == "banana"
    puts "バナナを渡す"
  elsif lemmon == "lemmon"
    puts "レモンを渡す"
  end

  apple

end

binding.pry

buy_fruits

~実際の表示~
34:
35: end
36:
37: binding.pry
38:
=> 39: buy_fruits

[1] pry(main)> buy_fruits
りんごを渡す
=> "apple"

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?