1
0

【Ruby】問題集解答 Part4(paizaラーニング)

Posted at

カンマ区切りの3つのデータの入力 (paizaランク D 相当)

自分の解答

practice_01.rb
inputs = gets.chomp.split(",")

inputs.each do |input|
  puts input
end

入力を受け取ると同時にカンマ区切りで変数に格納し、eachメソッドで出力する。

ChatGPTの解答

以下は、問題を解決するためのRubyプログラムの例です。

ruby
# 入力を受け取る
input_str = gets.chomp

# カンマで分割して各文字列を取得
strings = input_str.split(',')

# 順に3行で出力
strings.each do |str|
  puts str
end

このプログラムは、標準入力から1行の文字列を受け取り、カンマで分割して配列に格納します。そして、その配列を順に取り出して各文字列を3行で出力します。

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