Skip to content

Instantly share code, notes, and snippets.

@amikula
Created February 17, 2009 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amikula/65995 to your computer and use it in GitHub Desktop.
Save amikula/65995 to your computer and use it in GitHub Desktop.
# Module for re-running commands from the history in irb and its ilk (script/console)
# Usage:
# history => view the history
# history 7 => rerun line 7 in the history
# history /foo/ => rerun the last line in the history matching /foo/
#
# Just add this code into your ~/.irbrc.
# NOTE: Readline support is required for this to work.
module HistoryRepeats
class << self
def history(repeat=nil)
case repeat
when Integer
re_run {Readline::HISTORY[repeat]}
when Regexp
re_run {Readline::HISTORY.to_a.reverse.detect{|l| l =~ repeat}}
when nil
Readline::HISTORY.each_with_index do |line, i|
puts "#{i} #{line}"
end
nil
end
end
private
def re_run
# Remove the history command from the history
Readline::HISTORY.pop
command = yield
# Let the user know what we're executing
puts command
# Put the new command into the history...
Readline::HISTORY.push(command)
# ...and execute it in IRB's CurrentContext
IRB.CurrentContext.evaluate(command, IRB.CurrentContext.instance_eval('@line_no'))
end
end
end
def history(*args)
HistoryRepeats.history(*args)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment