2
1

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.

Ruby 複数行の入力を受け取る

Posted at

複数行の入力を受け取る

input = readlines

# 入力
# 120
# 115
# 122
# 116
# 119
# 117

puts input
# 出力結果
# 120
# 115
# 122
# 116
# 119
# 117 

readlinesメソッドで複数行受け取ることができる

複数行の入力を改行なしで受け取る

input = readlines.map &:chomp

# 入力
# 120
# 115
# 122
# 116
# 119
# 117

print input 
# 出力結果
# ["120", "115", "122", "116", "119", "117"]

puts input
# 出力結果
# 120
# 115
# 122
# 116
# 119

・chompメソッド: 文字列の改行を取り除く。
・mapメソッドで要素を整数に変換する

2
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?