Kernel#gets attempts to read file instead of standard input

前端 未结 2 1250
余生分开走
余生分开走 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:22

    You should be using STDIN.gets not Kernel#gets.

    Some reference.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题