Skip to content

Instantly share code, notes, and snippets.

@retr0h
Forked from amikula/gist:65995
Created February 17, 2009 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retr0h/66008 to your computer and use it in GitHub Desktop.
Save retr0h/66008 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
eval(command)
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