Created
February 17, 2009 21:35
-
-
Save amikula/65995 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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