Ruby

Getting stdin (Standard Input) in Ruby

This article introduces several ways to get stdin (standard input) in Ruby. For clarity, we're using the command line below, but it works the same way in actual Ruby language scripts. seq 3 | ruby -e "puts STDIN.gets...

Shou Arisaka
1 min read
Nov 6, 2025

This article introduces several ways to get stdin (standard input) in Ruby. For clarity, we’re using the command line below, but it works the same way in actual Ruby language scripts.

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$

Ah… so easy…

Share this article

Shou Arisaka Nov 6, 2025

🔗 Copy Links