问题
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
回答2:
I know this question has been answered. I am also new to Ruby but the tutorial is used did it this way. It used $stdin.gets.chomp
So your code will be game.the_word = $stdin.gets.chomp
According to my research, gets.chomp
will first check for content in the ARGV and if it doesn't find any it uses the standard input. Doing it that way will read the user input directly. :)
来源:https://stackoverflow.com/questions/40567901/rspec-issue-believed-to-be-with-gets-chomp-call