My ruby input practice won't work. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

My ruby input practice won't work.

In my text editor, I wrote the following: puts "Enter your name" name = gets.chomp puts "Welcome, #{name}" When I execute the code as an .rb file, it displays "Enter your name", but as soon as I do so, it crashes. Can you help me? I have no idea what the problem could be.

2nd Nov 2016, 7:57 AM
Arthur Dent
Arthur Dent - avatar
6 Answers
+ 2
What you posted seems to be correct and worked when I tried it. If it will not set you back too much, Arthur, try exiting and restarting your editor. It's rare but I've had a couple editors freeze up and throw errors with perfectly good code due to other processes clogging up the works so to speak.
3rd Nov 2016, 5:02 AM
Andre Burte
Andre Burte - avatar
+ 1
When you say "it crashes" what error messages are you seeing? What are you using to run your programme?
2nd Nov 2016, 10:37 PM
Ben
0
Oh, now I get it. The programme actually does work, but cmd closes anyway after "Welcome, #{name}" is displayed. Can I prevent cmd from closing, even when the programme already has finished? (I'm sorry about grammar mistakes btw, I'm not a native speaker.)
3rd Nov 2016, 3:15 PM
Arthur Dent
Arthur Dent - avatar
0
Just add another gets at the bottom of the script. It'll wait there till you press return.
3rd Nov 2016, 3:53 PM
Ben
0
Thank you!
3rd Nov 2016, 3:56 PM
Arthur Dent
Arthur Dent - avatar
0
It seems like you double click the ruby script file. Instead issue the following command in cmd shell. ruby filename.rb
19th Nov 2016, 11:08 AM
Abhishek Gedam
Abhishek Gedam - avatar