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?

More than 3 years have passed since last update.

【Ruby】標準入力から値を取得する方法まとめ【Paizaスキルチェック対策】

Posted at

一行から値を取得する

# ========================================
パターン1 変数にgetsメソッドを入れ、putsで出力
# ========================================
n = gets
puts n


# ========================================
パターン2 stdinを使う方法
# ========================================
puts $stdin.gets

複数行から要素を取得して出力

標準入力
2
4
7

# ========================================
パターン1
# ========================================


# ========================================
パターン2
# ========================================
puts $stdin.readlines 

readlinesはstdinで取得した複数行の値を「配列」として読み取る。
一度n = $stdin.readlines と変数に入れておきputs n[0]とかする事も可能。
*文字列として読み取りたい場合はread.splitを使う。

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?