简体   繁体   English

rspec 问题被认为与 gets.chomp 调用有关

[英]rspec issue believed to be with gets.chomp call

I am having trouble or am confused with an rspec error when running:我在运行时遇到问题或对 rspec 错误感到困惑:

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.当我在没有 -fd 的情况下运行它时,它可以工作……但它看起来很傻……出了点问题。 I start getting error messages when I use gets.chomp but I've looked at similar examples that use it.当我使用gets.chomp 时,我开始收到错误消息,但我查看了使用它的类似示例。 Does 'the_word' have to be set in initialize? 'the_word' 必须在初始化中设置吗? Does gets.chomp or can it?是gets.chomp还是可以? I appreciate any help.我很感激任何帮助。 Below is game file and its rspec so far.以下是到目前为止的游戏文件及其 rspec。 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!谢谢!

有问题的用户界面rspec 文件

The reason is that gets reads from ARGV (argument variables), such as the -fd flag you're giving to rpsec .原因是从ARGV (参数变量)读取,例如您提供给gets-fd 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.您可以通过在调用gets.之前清除ARGV来防止这种情况发生。

> 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.你不能只说ARGV = []因为它是一个常量,但是调用clear很好,因为它没有重新定义变量。

In short, just put this somewhere before gets.chomp :简而言之,只需将其放在gets.chomp之前的某个位置:

ARGV.clear

Here's another question on the topic: Ruby readline fails if process started with arguments这是关于该主题的另一个问题: Ruby readline failed if process started with arguments

I used $stdin.gets.chomp , so the code will be:我使用$stdin.gets.chomp ,所以代码将是:

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.根据我的研究, gets.chomp将首先检查 ARGV 中的内容,如果找不到,它会使用标准输入。 Doing it that way will read the user input directly.这样做会直接读取用户输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM