Using Ruby's Readline Library
Posted by Adam Wiggins on January 22, 2008 at 08:48 PM
There appears to be no documentation for Ruby's readline support. What's worse, it's written in C, so you can't (easily) read the source to find out its interface.
By peering at IRB's source, however, I was able to construct the following:
require 'readline' loop do line = Readline::readline('> ') Readline::HISTORY.push(line) puts "You typed: #{line}" end
Comments
There are 3 comments on this post. Post yours →
I don't get it. Why isn't the input value in "line". Why is this so damned obscure? This is a bloody waste of time, for me at least. (To publish software without documentation.)
My guess about the lack of documentation is that the interface is very similar to the C API for readline. For someone who already has familiarity with using it in other languages, it should be very straightforward.
man readline
For someone unfamiliar with it in C, I can understand the confusion. Personally, I'm glad that I found this blog post because I was just looking for an example to paste ;-).
http://svn.ruby-lang.org/repos/ruby/tags/v186_111/ext/readline/README has some basic docs.
Readline.readline takes a second boolean argument for history, so this will work as well:
require 'readline'
loop do line = Readline::readline('> ', true) puts "You typed: #{line}" end
Post a comment
Required fields in bold.