Ruby

Rubyでstdin(標準入力)を取得する

Rubyでstdin(標準入力)を取得するいくつかの方法について紹介します。以下ではわかりやすいようにコマンドラインを使用していますが、実際のRuby言語のスクリプトでも同じように動作します。seq 3 | ruby -e "puts STDIN.gets…

Shou Arisaka
1 分で読める
2019年3月18日

Rubyでstdin(標準入力)を取得するいくつかの方法について紹介します。以下ではわかりやすいようにコマンドラインを使用していますが、実際のRuby言語のスクリプトでも同じように動作します。

ASUS:/mnt/c/pg/20190111030052$ seq 3 | ruby -e "puts STDIN.gets"
1
ASUS:/mnt/c/pg/20190111030052$ seq 3 | ruby -e "puts STDIN.read"
1
2
3
ASUS:/mnt/c/pg/20190111030052$ seq 3 | ruby -e "puts STDIN.read + 'hogehoge'"
1
2
3
hogehoge

e.g.

ASUS:/mnt/c/pg/20190111030052$ printf " hello\n world\n  " | ruby -e "puts STDIN.read.strip"
hello
world
ASUS:/mnt/c/pg/20190111030052$ printf " hello\n world\n  " # | ruby -e "puts STDIN.read.strip"
hello
world
ASUS:/mnt/c/pg/20190111030052$

あー…楽だ…。

この記事をシェア

Shou Arisaka 2019年3月18日

🔗 リンクをコピー