Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Keep console open after script execution

Tags:

ruby

console

I wrote a Ruby script like the following example. The basic functionality is the same:

# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)

The problem is when I run the script it prints the solution I want, but the console window closes right after. I want the console to keep open. I'm currently on windows, but the solution should be working on every system.

One way is to run the ruby script with a .bat file and pause it, like so:

ruby script.rb
PAUSE

I hope there is a way without the additional .bat file. Does Ruby has a function like PASUE integrated?

like image 376
ph3nx Avatar asked Jan 19 '14 10:01

ph3nx


2 Answers

It seems like you double click the ruby script file.

Instead issue the following command in cmd shell.

ruby filename.rb

If you don't want that, you can add gets to the end of the script.

# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
gets # <----

But this is not recommended because .. if you run the command in cmd shell or terminal you should type extra Enter to return to the shell.

like image 199
falsetru Avatar answered Oct 18 '22 01:10

falsetru


Use the -r options of irb.

irb -r ./filename.rb

like image 25
Paul Verschoor Avatar answered Oct 18 '22 01:10

Paul Verschoor