DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Irb Itches Eliminated

Snippets Manager user avatar by
Snippets Manager
·
Oct. 10, 05 · · Code Snippet
Like (0)
Save
Tweet
490 Views

Join the DZone community and get the full member experience.

Join For Free
I use the following code to print out, edit and re-eval
irb history. A more detailed explanation at: 
http://www.chwhat.com/articles/2005/10/08/


require 'tempfile'
HISTFILE = "~/.irb.hist"
MAXHISTSIZE = 300

#borrowed from http://rubygarden.org/ruby?Irb/TipsAndTricks
begin
	if defined? Readline::HISTORY
		histfile = File::expand_path( HISTFILE )
		if File::exists?( histfile )
			lines = IO::readlines( histfile ).collect {|line| line.chomp}
			puts "Read %d saved history commands from %s." %
				[ lines.nitems, histfile ] if $DEBUG || $VERBOSE
			Readline::HISTORY.push( *lines )
		else
			puts "History file '%s' was empty or non-existant." %
				histfile if $DEBUG || $VERBOSE
		end

		Kernel::at_exit {
			lines = Readline::HISTORY.to_a.reverse.uniq.reverse
			lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE
			$stderr.puts "Saving %d history lines to %s." %
				[ lines.length, histfile ] if $VERBOSE || $DEBUG
			File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) {|ofh|
				lines.each {|line| ofh.puts line }
			}
		}
	end
end

## My stuff
class Array
	def multislice(range,splitter=',',offset=nil)
		result = []
		for r in range.split(splitter)
		if r =~ /-/
			min,max = r.split('-')
			slice_min = min.to_i - 1
			slice_min += offset if offset
			result.push(*self.slice(slice_min, max.to_i - min.to_i + 1))
		else
			index = r.to_i - 1
			index += offset if offset
			result.push(self[index])
		end
		end
		return result
	end
end

$original_history_size = Readline::HISTORY.size
def history_list(first_num,second_num=Readline::HISTORY.size - 1)
	Readline::HISTORY.to_a[(first_num + $original_history_size - 1) .. (second_num + $original_history_size - 1) ]
end
def history_slice(nums)
	Readline::HISTORY.to_a.multislice(nums,',',$original_history_size)
end
def history_list_or_slice(*args)
	if args[0].class == String
		history_slice(*args)
	else
		history_list(*args)
	end
end
def print_history(*args)
	puts history_list_or_slice(*args).join("\n")
end
def eval_history(*args)
	eval %[ #{history_list_or_slice(*args).join("\n")} ]
end
def edit_history(*args)
	history_string = history_list_or_slice(*args).join("\n")
	edit(history_string)
end
def edit(string=nil,editor=ENV['EDITOR'])
	editor ||= raise "editor must be given or defined by EDITOR environment variable"
	tempfile = Tempfile.new('edit')
	File.open(tempfile.path,'w') {|f| f.write(string) } if string
	system("#{editor} #{tempfile.path}")
	File.open(tempfile.path) {|f| f.read } 
end

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Maven Tutorial: Nice and Easy [Video]
  • Top Six Kubernetes Best Practices for Fleet Management
  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • Memory Debugging and Watch Annotations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo