ruby readline filename tab completion

Navigating the ancient Readline interface is a bit complicated. Here’s how to get filename completion when you hit the tab button:

require 'readline'
def ask_for_filename question, start_dir=""
  Readline.completion_append_character = nil
  Readline.completion_proc = lambda do |prefix|
    files = Dir["#{start_dir}#{prefix}*"]
      map { |f| File.expand_path(f) }.
      map { |f| ? f + "/" : f }
  Readline.readline question

To reply to the article, enter your email address. A copy of the article will be sent to you via email.