8
1

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 5 years have passed since last update.

[Ruby]returnについて

Last updated at Posted at 2018-03-21

#return文の勉強中に気になったことがあったのでメモっとく
return文とは、メソッドの中で return 〜 と書いた時点の値(この場合は〜の値)を戻り値とする文で、returnが出た時点でメソッドは終了となります。

def hoge
  "a"
  "b"
  "c"
  return "d"
  "e"
  "f"
  "g"
end

puts hoge
# =>d が戻り値となる

return文が無い場合は、最後に出てきた g が戻り値となります。

##気になったこととは
メソッド内で色々と値が発生したとしても、最後に発生した値がメソッドの戻り値なんだなと、、当然かもだけど気になった。

8
1
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
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?