rspec issue believed to be with gets.chomp call
Asked Answered
S

2

0

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

rspec -fd game_spec

respec error messaage

(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!

user interface with issue rspec file

Substation answered 12/11, 2016 at 21:36 Comment(1)
by the way, it's preferrable to add a code block to the question than to use a screenshotTriable
T
2

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

Triable answered 12/11, 2016 at 21:49 Comment(3)
thanks a bunch. so where exactly do you place it before it? like this? 'code' puts "Please STRATEGICALLY provide me with a word, player one." game.the_word = ARGV.clear gets.chomp.split('')'code'Substation
put it on its own line, before the game.the_word assignment and gets.chomp call. If you're not using ARGV in your code (and you don't seem to be), you can put it at the top of the file.Triable
got it! awesome. much appreciated ARGV.clear; gets.chomp.split('')Substation
I
0

I used $stdin.gets.chomp, so the 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.

Immunogenetics answered 4/12, 2019 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.