LoginSignup
0
0

More than 1 year has passed since last update.

Ruby6

Last updated at Posted at 2023-01-29

問題

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

明日の天気は晴れです

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

呼び出し方:get_weather_forecast("晴れ")




模範解答

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

get_weather_forecast("晴れ")

【解説】

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

メソッドの呼び出し
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