LoginSignup
1
3

More than 5 years have passed since last update.

半角空白ありの一列の数値の入力値を変数に入れる

Posted at

分解しないで変数に入れる場合

配列aに数値群をそのまま入れる。

# input => 1 2 3
a = gets.split.map(&:to_i)
p a
# output => [1,2,3]

1つずつに分解して変数に入れる場合

個数がもし決まって入れば、.mapして複数の変数に入れたいとき、変数をカンマで区切ることができる。
splitはデフォルトで空白で区切ってくれる。

# input => 1 2 3
s1,s2,s3=gets.split.map(&:to_i) 
p s2 + s3
# output => 5

.mapの部分を分解するとこう。

.map{|item| item.to_i}
1
3
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
3