# # history for irb # # Copyright (c) 2001 akira yamada # You can redistribute it and/or modify it under the same term as Ruby. # # Version 0.0.0.0.1. # # Special Thanks: # * Satoru Takabayashi # An idea and some comments. # begin require 'readline' class HisReadlineInputMethod < IRB::ReadlineInputMethod HISTORY_FILE = '~/.irb_history' MAX_HISTORY_LINES = 50000 def HisReadlineInputMethod.finilizer(path, history) proc { skip_history = 0 if history.length > MAX_HISTORY_LINES skip_history = history.length - MAX_HISTORY_LINES end open(path, 'w') {|io| io.flock(File::LOCK_EX) history.each {|x| if skip_history > 0 skip_history -= 1 next end io.puts x } io.truncate(io.pos) } } end include Readline def initialize super begin @history_file = File.expand_path(HISTORY_FILE) open(@history_file) {|io| io.flock(File::LOCK_EX) io.each {|line| @line << line HISTORY << line.chomp } } @line_no = @line.size rescue Errno::ENOENT ensure ObjectSpace.define_finalizer(self, HisReadlineInputMethod.finilizer(@history_file, HISTORY)) end end end IRB.conf[:SCRIPT] = HisReadlineInputMethod.new rescue LoadError end