Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get unstuck in Ruby irb? [duplicate]

Tags:

ruby

irb

Possible Duplicate:
Is there a way to get out of a “hung” state in IRB?

I am using IRB. When I am coding I noticed that I get "stuck" when the line ends with "/":

irb(main):057:0/

When that happens I cannot do anything, I can't exit, define things, etc. It keeps looping back to lines that end in "/".

But, when a line ends like this:

irb(main):056:0>

everything seems to work fine. I can exit if needed, define anything, etc.

How can I get unstuck when a line ends in "/"?

like image 1000
user1804592 Avatar asked Nov 06 '12 23:11

user1804592


3 Answers

You can press ctrl + c followed by return to get back IRB's prompt.

like image 115
alex Avatar answered Sep 28 '22 04:09

alex


When you are 'stuck' in IRB it is usually because of an unmatched closing delimiter, such as a single quote not matching a double quote.

For this specific question it is due to being in a Regexp object, of which the delimiter is a '/' that you have pressed. This is identical in action to having an open quote that you don't close. As soon as you end the forward slash to close the Regexp, you will find yourself on the next prompt, and you will see some return from IRB on the line prior to your cursor location. So, it is appropriate to simply close the delimiter.

Perhaps, and this can only be a fishing expedition, you meant to ignore the return at the end of the line instead, and when you should have used the backslash ('\') you used the forward slash?

Control-C is kind of heavy handed, as it attempts to send an interrupt. Control-D is the EOD or End of Data* character, and so will generally let IRB know that you are done inputting data on the line (or stream).

This works for more than simply IRB and can get you out of some pretty tough places, without terminating the application that is running. Allowing you to have a graceful exit, or even continue running the program, and correcting your mistake, such as happens sometimes in IRB.

Screenshot of situation where missing one of the parenthesis are missing.

Of course, if that fails, then try control-c it will likely be just heavy handed enough to get you through.


*: historically, EOT or "End of Tape" or "End of Transmission". It may be simply my mnemonic to relate it to 'data' as in a stream of input.

like image 20
vgoff Avatar answered Sep 28 '22 03:09

vgoff


That's caused by hitting Enter when the line ends in an unterminated regex. You can either use Ctrl+C like alex said, or complete the regexp by ending it with another slash (or, if you began the regexp with %{, end it with a }, etc.)

like image 30
echristopherson Avatar answered Sep 28 '22 03:09

echristopherson