rspec issue believed to be with gets.chomp call

2019-09-14 19:23发布

问题:

I am having trouble or am confused with an rspec error when running:

rspec -fd game_spec

(the above is only the beginning of a long eror message)

When I run it without the -fd, it works... but it looks goofy... something is wrong. I start getting error messages when I use gets.chomp but I've looked at similar examples that use it. Does 'the_word' have to be set in initialize? Does gets.chomp or can it? I appreciate any help. Below is game file and its rspec so far. I do not want to change much at all. I just want to get what I have so far working and keep pushing on. Thanks!

回答1:

The reason is that gets reads from ARGV (argument variables), such as the -fd flag you're giving to rpsec.

You can recreate this situation pretty easily:

> ruby -e "puts gets.chomp" ASD
-e:1:in `gets': No such file or directory @ rb_sysopen - ASD (Errno::ENOENT)
    from -e:1:in `gets'
    from -e:1:in `<main>'

You can prevent this from happening by clearing out ARGV before calling gets.

> ruby -e "ARGV.clear; puts gets.chomp" ASD
asd # <-- I then type this
asd # <-- and this is printed

You can't just say ARGV = [] because it's a constant, but calling clear is fine because it's not redefining the variable.

In short, just put this somewhere before gets.chomp:

ARGV.clear

Here's another question on the topic: Ruby readline fails if process started with arguments