rspec issue believed to be with gets.chomp call

浪子不回头ぞ 提交于 2019-12-25 04:13:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!