irb history. A more detailed explanation at:
http://www.chwhat.com/articles/2005/10/08/
require 'tempfile' HISTFILE = "~/.irb.hist" MAXHISTSIZE = 300 #borrowed from http://rubygarden.org/ruby?Irb/TipsAndTricks begin if defined? Readline::HISTORY histfile = File::expand_path( HISTFILE ) if File::exists?( histfile ) lines = IO::readlines( histfile ).collect {|line| line.chomp} puts "Read %d saved history commands from %s." % [ lines.nitems, histfile ] if $DEBUG || $VERBOSE Readline::HISTORY.push( *lines ) else puts "History file '%s' was empty or non-existant." % histfile if $DEBUG || $VERBOSE end Kernel::at_exit { lines = Readline::HISTORY.to_a.reverse.uniq.reverse lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE $stderr.puts "Saving %d history lines to %s." % [ lines.length, histfile ] if $VERBOSE || $DEBUG File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) {|ofh| lines.each {|line| ofh.puts line } } } end end ## My stuff class Array def multislice(range,splitter=',',offset=nil) result = [] for r in range.split(splitter) if r =~ /-/ min,max = r.split('-') slice_min = min.to_i - 1 slice_min += offset if offset result.push(*self.slice(slice_min, max.to_i - min.to_i + 1)) else index = r.to_i - 1 index += offset if offset result.push(self[index]) end end return result end end $original_history_size = Readline::HISTORY.size def history_list(first_num,second_num=Readline::HISTORY.size - 1) Readline::HISTORY.to_a[(first_num + $original_history_size - 1) .. (second_num + $original_history_size - 1) ] end def history_slice(nums) Readline::HISTORY.to_a.multislice(nums,',',$original_history_size) end def history_list_or_slice(*args) if args[0].class == String history_slice(*args) else history_list(*args) end end def print_history(*args) puts history_list_or_slice(*args).join("\n") end def eval_history(*args) eval %[ #{history_list_or_slice(*args).join("\n")} ] end def edit_history(*args) history_string = history_list_or_slice(*args).join("\n") edit(history_string) end def edit(string=nil,editor=ENV['EDITOR']) editor ||= raise "editor must be given or defined by EDITOR environment variable" tempfile = Tempfile.new('edit') File.open(tempfile.path,'w') {|f| f.write(string) } if string system("#{editor} #{tempfile.path}") File.open(tempfile.path) {|f| f.read } end