Rohan Dalvi
Rohan Dalvi

Reputation: 1265

Ruby "gets" does not wait for user input

This is a very silly question, but it doesn't work for me.

I am trying to make the program wait for my input. I tried replacing gets with stdin.gets, and $stdin.gets and when I try gets.chomp I get a nil class exception.

puts "Get works here?"
option = gets
puts option

Upvotes: 4

Views: 11925

Answers (2)

Michael Kramer
Michael Kramer

Reputation: 11

my script was also not waiting for input from gets(), but started to do so when I used

$stdin.gets("\n")

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118261

To work this,you need to call your .rb file from your command prompt. Like say you save your code in a file called test.rb.

test.rb

puts "Get works here?"
option = gets
puts option

Then run from your command prompt:

C:\Users\arup> ruby test.rb

Upvotes: 2

Related Questions