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?

唯一のメンターであるAIに聞いた、gets.split.map(&:to_i)に関する解説

Last updated at Posted at 2024-10-23

先生の解説

# 1.入力を取得
input = gets.chomp # ユーザーからの入力を取得
# 2.空白で分割し、配列に変換
split_input = input.split 
# 3.各要素を整数に変換
ns = split_input.map { |num| num.to_i } 

# これで ns に整数の配列が格納される

説明
1.入力の取得: gets.chomp で、ユーザーからの入力を取得し、末尾の改行を削除します。

2.分割: input.split によって、取得した文字列を空白で分割し、配列に変換します。例えば、入力が "2 3 5" であれば、split_input には ["2", "3", "5"] が格納されます。

3.整数への変換: map { |num| num.to_i } を使って、各文字列要素を整数に変換し、最終的に整数の配列 ns に格納します。

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?