开发者

Ruby Switch Between File and Standard Input

开发者 https://www.devze.com 2022-12-16 17:21 出处:网络
How would you create a variable that could be read. It would read from a certain file if it exists, otherwise it would read from standard input. Something like:

How would you create a variable that could be read. It would read from a certain file if it exists, otherwise it would read from standard input. Something like:

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

This doesn't work, but I think this should be done pret开发者_如何转开发ty often, but I can't find a nice way to do it.


This this works for you?

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


See: ...run against stdin if no arg; otherwise input file =ARGV


I think ruby has the ability to treat arguments that aren't used before STDIN is first used as if it were filenames for files piped into standard input.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号