Kernel#gets attempts to read file instead of standard input

前端 未结 2 1251
余生分开走
余生分开走 2020-12-15 03:19

I\'m rather new to Ruby, and today I wrote an application which you can pass arguments to via ARGV. But inside my application, I want to be able to use \"gets\". Problem is,

2条回答
  •  一生所求
    2020-12-15 03:44

    If there are arguments, the default gets method tries to treat the first one as a file and read from that. To read from $stdin in such a situation, you have to use it explicitly:

    print "Say something! "
    input = $stdin.gets
    

提交回复
热议问题