Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you save IRB inputs to a .rb file?

Tags:

ruby

irb

watir

Might sound like a newbie question (and it is since I am new to Ruby and IRB) but is there a way to save the commands you did in IRB to file? I am playing with WATIR and would love to save all my inputs to file instead of copying and pasting each.

Thanks for the help!

like image 676
Enrique Avatar asked Nov 19 '10 21:11

Enrique


2 Answers

On my machine I can put this in my .irbrc file (located in your home directory):

Kernel.at_exit {
  File.open("irb.log", "w") do |f|
    f << Readline::HISTORY.to_a.join("\n")
  end
}

It creates a file irb.log that contains your readline history. Irb uses readline for command input. It might be configured not to use readline for some people, I don't know. And maybe the history will be truncated at some point, or maybe it'll be modified by certain commands you do in your irb session... but try it out and see if it works.

If you want the irb prompt and the result of each command to be included in the log, then just use tee to record the output of irb:

$ irb | tee irb.log
like image 138
Paige Ruten Avatar answered Sep 30 '22 14:09

Paige Ruten


You can run vim in irb:

http://vimcasts.org/episodes/running-vim-within-irb/

like image 42
Petrik de Heus Avatar answered Sep 30 '22 15:09

Petrik de Heus