Robert A Henru Robert A Henru - 1 year ago 108
Ruby Question

Can I repeat command in irb?

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.

Answer Source
def repeat_last_irb

then you can use replat_last_irb in you irb console to run last input. 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::ReadlineInputMethod:0x00000001c7b860 @file_name="(line)", @line_no=3, @line=[nil, "def hello\n", "end\n", "\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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download