Ruby standard input

Introduction

This time I often use standard input for programming problems, so I'll leave a note. The language used is Ruby.

For strings

When there is only one element per line

Standard input
Tokyo
line = gets
p line
Output result
"Tokyo"

When there are multiple elements in one line

Standard input
Tokyo Osaka Kyoto
line = gets.split(' ')
p line
Output result
["Tokyo", "Osaka", "Kyoto"]

By using the split method, the three elements are stored in the array as separate ones.

When elements exist one by one in multiple lines

Standard input
Tokyo
Osaka
Kyoto
line = readlines
len = line.length
i = 0

while i < len
    line[i] = line[i].chomp
    i += 1
end

p line 
Output result
["Tokyo", "Osaka", "Kyoto"]

A concise way to write the above is to use the map method.

Standard input
Tokyo
Osaka
Kyoto
line = readlines.map(&:chomp)
p line 
Output result
["Tokyo", "Osaka", "Kyoto"]

When there are multiple elements in multiple lines

Standard input
Tokyo Osaka Kyoto
Japan USA China
line = readlines
len = line.length
i = 0

while i < len
    line[i] = line[i].chomp.split(' ')
    i += 1
end

p line
Output result
[["Tokyo", "Osaka", "Kyoto"], ["Japan", "USA", "China"]]

There is also the following as a concise way of writing the above

Standard input
Tokyo Osaka Kyoto
Japan USA China
lines = []
while line = gets
    lines << line.chomp.split(' ')
end
p lines
Output result
[["Tokyo", "Osaka", "Kyoto"], ["Japan", "USA", "China"]]

By setting while line = gets, it will be repeated until all the standard input values are acquired.

Standard input
Tokyo Osaka Kyoto
Japan USA China
lines = readlines(chomp: true).map{|line| line.split(' ')}
p lines
Output result
[["Tokyo", "Osaka", "Kyoto"], ["Japan", "USA", "China"]]

Looking at the output result, the received value is a character string, so if you want to receive a numerical value, do as follows

If you want to receive it numerically

There is only one element per line

Standard input
123
line = gets.to_i
p line
Output result
123

When there are multiple elements in one line

Standard input
1 2 3
line = gets.split(' ')
p line
Output result
["1", "2", "3"]

If this is left as it is, it will be treated as a character string, so use map to convert it to a numeric type array.

Standard input
1 2 3
line = gets.split(' ').map(&:to_i)
p line
Output result
[1, 2, 3]

When there is one element in multiple lines

Standard input
1
2
3
line = readlines.map(&:to_i)
p line 

Output result
[1, 2, 3]

When there are multiple elements in multiple lines

Standard input
1 2 3
4 5 6
lines = []
while line = gets
    lines << line.chomp.split(' ').map(&:to_i)
end
p lines
Output result
[[1, 2, 3], [4, 5, 6]]

Here's how to write more concisely

Standard input
1 2 3
4 5 6
lines = readlines(chomp: true).map{|line| line.split(' ').map(&:to_i)}
p lines
Output result
[[1, 2, 3], [4, 5, 6]]

Finally

I think there are various other methods, so I will update them from time to time.

Recommended Posts

Ruby standard input
Ruby input / output
Ruby standard input and various methods
Ruby standard output
From terminal to Ruby (standard input / output)
Ruby receives multi-line input
[ruby] How to receive values from standard input?
Read standard input in Java
Ruby Learning # 10 Getting User Input
[Ruby] Receive input from console
Learning Ruby with AtCoder 11 How to receive frequently used standard input
Ruby learning 4
Story of paiza.jp [Java standard input solution]
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
[Rails] Get standard input for multi-line data
Ruby setting 2
Ruby problem ⑦
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
[Ruby] Recognize ASCII control characters such as ^ H as intended when accepting standard input
[Java] How to get and output standard input
A story packed with Java's standard input Scanner
About the standard input stream (System.in) once closed
Ruby on Rails address automatic input implementation method
[Ruby] Summary of how to get values from standard input [Paiza skill check measures]