Reputation: 21
Whenever I accidently type the wrong syntax in IRB, I can never go back and fix it and it keeps giving new command lines.
For example:
1.9.3p194 :035> justin.find{|key,value|key=='phone_number
1.9.3p194 :036> justin.find{|key,value|key=='phone_number'}
1.9.3p194 :037>
1.9.3p194 :038>
1.9.3p194 :039>
After I fixed the typo in my syntax as seen on line :036 in the example, it doesn't execute the method.
Upvotes: 2
Views: 1822
Reputation: 3734
I have an apostrophe after the line number :
1.8.6 :200 > justin.find{|key,value|key=='phone_number
1.8.6 :201'>
See if you can configure the prompt, so that you get a clue about what IRB is expecting in case of continued statement. See also What is the class of "if/unless" etc
Upvotes: -1
Reputation: 2007
Everyone has pointed the cause, no one answered the question.
Use Control + C to end the wrong line.
Without executing, of course, it has errors in it.
Once you are on a new line you can use the up key to searhc it in history to edit it.
By the way, is strange that you irb does not tell in any way that your line has not been executed:
As you can see, my irb prompts for the special char to end the command (probably if i had multiple parens/quotes, etc, it would point for the first one:
irb(main):001:0> justin.find{|key,value|key=='phone_number
irb(main):002:1' ^C
(Up Key)
irb(main):002:0> justin.find{|key,value|key=='phone_number'
irb(main):003:1>
Windows version (ruby 1.9.3p327 (2012-11-10) [i386-mingw32] what i got in job :( ) But im sure i got something similar in Gentoo and Suse
Edit:
From related posts on right. You can get a better prompt passing as option
irb --prompt inf-ruby
How do I get the "irb(main):001:0>" prompt instead of ">>"
Upvotes: 6
Reputation: 11726
Disregard Stack Overflow's syntax highlighting in this answer. It is misleading.
The line
justin.find{|key,value|key=='phone_number
contains a beginning of a multiline string, which starts with phone_number
. Since you haven't finished it yet—by entering a second '
—IRB politely waits for you to finish the string. Entering
justin.find{|key,value|key=='phone_number'}
as the second line results in finishing the string you have started, entering a method call or local variable phone_number
and right afterwards adding a begining of yet another string, now starting with }
. At this point the code which IRB sees looks like
justin.find{|key,value|key=="phone_number\njustin.find{|key,value|key=="phone_number'}
It is not finished yet—because the last string is not terminated—so IRB waits for more input.
Each subsequent
justin.find{|key,value|key=='phone_number'}
makes you stay in this loop.
Ruby will not interrupt you and tell that there's a syntax error. The reason is that IRB operates on a per-line¹ basis and you haven't technically finished the line. Indeed, the strings in the expression have new line characters in them, but there's no newline character in the code which would cause the expression to terminate.
Now I hope that the answer to your question is obvious. In order to get IRB out of the loop enter
'
¹ Per-expression would be more precise, but per-line is satisfactory in the context of this answer.
Upvotes: 1