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

引数の基礎

Posted at

"晴れ" という文字列を引数で渡した時に

明日の天気は晴れです

とターミナルに表示されるようなメソッドを作成。

呼び出し方:get_weather_forecast(“晴れ”)

def get_weather_forecast(weather)
  puts "明日の天気は#{weather}です"
end

get_weather_forecast("晴れ")

「明日の天気は〇〇です」の「〇〇」に任意の文字列(今回は「晴れ」)を表示したいので、引数を使用します。
「〇〇」にはメソッドの呼び出し元の引数に渡した文字列が表示されます。

get_weather_forecast("晴れ")

そして、文字列を出力するためのget_weather_forecastメソッドを定義します。
引数で渡した文字列を表示させたいので、文字展開を使用しています。

def get_weather_forecast(weather)
  puts "明日の天気は#{weather}です"
end
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?