Is there an easy way to repeat a previous command in Ruby irb? I wish to have something like using exclamation mark (!) in Unix.
Thank you.
To invoke it, type irb at a shell or command prompt, and begin entering Ruby statements and expressions. Use exit or quit to exit irb. Sr.No. Suppress reading of the file ~/.
The => symbol lets you know that this is the return value from the Ruby expression. To exit IRB, type exit at the prompt, or press CTRL+D . You'll return to your shell prompt. Let's dig a little deeper into IRB by looking at how you can use it to explore code.
IRB is the built-in Ruby REPL that every Ruby developer is familiar with. It doesn't have many features, but it does what it's supposed to. You type in code & you get the results back.
From the main menu, choose Tools | Load file/selection into IRB/Rails console.
def repeat_last_irb
eval(IRB.CurrentContext.io.line(-2))
end
then you can use replat_last_irb
in you irb console to run last input.
IRB.CurrentContext.io is like this below:
ruby-1.9.3-p0 :001 > def hello
ruby-1.9.3-p0 :002?> end
=> nil
ruby-1.9.3-p0 :003 > IRB.CurrentContext.io
=> #<IRB::ReadlineInputMethod:0x00000001c7b860 @file_name="(line)", @line_no=3, @line=[nil, "def hello\n", "end\n", "IRB.CurrentContext.io\n"], @eof=false, @stdin=#<IO:fd 0>, @stdout=#<IO:fd 1>, @prompt="ruby-1.9.3-p0 :003 > ">
this Object save irb all io info, and with a line method to get every line you input.
so, we can use eval to repeat last input.
Up arrow gets you history line by line. Pry has more goodies but I haven't tried it yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With