Allowing Input Editing with Readline
Problem
You want to let your users edit their lines of input as they write them, the way irb does.
Solution
Use the readline library. Instead of reading directly from standard input, pass a prompt string into Readline.readline. The user will be able to edit their input using the same shortcut keys you can use in the irb Ruby interpreter (assuming their terminal supports those keys).
#!/usr/bin/ruby -w # readline.rb require eadline vegetable = Readline.readline("Whats your favorite vegetable?> ") puts "#{vegetable.capitalize}? Are you crazy?"
Note that you don have to chomp the result of Readline.readline:
$ ruby readline.rb Whats your favorite vegetable?> okra Okra? Are you crazy?
On Windows, this isn necessary because the cmd shell provides any console program with many of readlines features. The example given above will work on both Windows and Unix, but if you e writing a Windows-specific program, you don need readline:
# readline_windows.rb print "Whats your favorite vegetable?> " puts gets.chomp.capitalize + "? Are you crazy?"
Discussion
In a Unix program that accepts data from standard input, the user can use their backspace key to correct typing mistakes, one character at a time. Backspace is a control character: its a real character, just like "1" and "m" (its Ruby string representation is "