Wirble のヒストリを最近やった順に辿れるようにする。

irb を使っているとき、ヒストリを検索すると、最近使った奴ほど先に出て欲しいので、.irbrc にこんなのを書きました。

module Wirble
  class History
    private
    def save_history
      path, max_size, perms = %w{path size perms}.map { |v| cfg(v) }

      # read lines from history, and truncate the list (if necessary)
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse # ← ココを変えただけ
      lines = lines[-max_size, -1] if lines.size > max_size

      # write the history file
      real_path = File.expand_path(path)
      File.open(real_path, perms) { |fh| fh.puts lines }
      say 'Saved %d lines to history file %s.' % [lines.size, path]
    end
  end
end

これでヒストリが近い順にのこるようになります。(過去に同じ行があるとき、一番新しい場所でヒットするようになる)

今思ったけど後ろから uniq っていうメソッドがあっても良さそうな。

require 'wirble' と Wirble.init(...) の間に書いておく必要がある気がする。