Standard input is the conventional input stream through which a Ruby process receives data from its environment. By default, Ruby treats keyboard input as standard input, and methods such as gets read from it. STDIN is an IO object representing this stream. section 12.1.3 section 12.1.1
Read a line or character
line = STDIN.gets char = STDIN.getc gets returns one line, while getc returns one character. Both operations are buffered: terminal input normally becomes available after Enter is pressed. Using STDIN explicitly continues to read the keyboard even if $stdin has been redirected. section 12.1.4
STDIN also supports IO and Enumerable behavior:
- Iteration normally uses the global input record separator $/; with its default newline value, each iteration reads one line. section 12.1.2
- Changing $/ changes what counts as one input record; setting it to "NEXT" makes iteration continue until that string appears. section 12.1.2
- Enumerable methods such as select and map can filter or transform records read from standard input. section 12.1.2
- STDIN is a constant for the original stream, while $stdin is a reassignable global variable used to change default input behavior. section 12.1.3
>> STDIN.each {|line| p line }
This is line 1
"This is line 1\n"
This is line 2
"This is line 2\n"
All separated by $/, which is a newline character
"All separated by $/, which is a newline character\n"
Shows line-by-line STDIN iteration, including the newline retained in each yielded string. section 12.1.2




