Ruby switch between file and standard input

How would you create a variable that could be read. It would read from a specific file if it exists, otherwise it would read from standard input. Sort of:

input = File.open("file.txt") || in

This does not work, but I think it needs to be done quite often, but I cannot find a good way to do this.

+3
source share
3 answers

Does this work for you?

input = File.exist?("file.txt") ? File.open("file.txt") : STDIN
+4
source

I think ruby ​​has the ability to process arguments that are not used before STDIN is first used as if they were file names for files connected to standard inputs.

+1
source

Source: https://habr.com/ru/post/1729054/


All Articles