class Dirby::History

Manages loading and saving input lines across runs of IRB.

Public Class Methods

new(opts = {}) click to toggle source
# File lib/dirby/history.rb, line 41
def initialize(opts = {})
  return unless defined? Readline::HISTORY
  load_history
  Kernel.at_exit { save_history }
end

Public Instance Methods

load_history() click to toggle source
# File lib/dirby/history.rb, line 21
def load_history
  real_path = File.expand_path("~/.irb_history")
  if File.exist?(real_path)
    lines = File.readlines(real_path).map {|line| line.chomp}
    Readline::HISTORY.push(*lines)
    puts "Read %d lines from history file %s" % [lines.size, real_path]
  else
    puts "No history to load."
  end
end
save_history() click to toggle source
# File lib/dirby/history.rb, line 32
def save_history
  lines = Readline::HISTORY.to_a.uniq
  real_path = File.expand_path("~/.irb_history")
  File.open(real_path, File::WRONLY | File::CREAT | File::TRUNC) do |f|
    f.puts lines
  end
  puts "Saved %d lines to history file %s." % [lines.size, real_path]
end