Use readline instead of gets

The simplest way to read a line of input is to use the gets method.
However, if you try to use the arrow keys when entering text, you will
see that this merely types random ^[[D strings instead of moving the

s = gets
press the left arrow^[[D
=> "press the left arrow\e[D\n"

To get around this, use readline instead:

> require 'readline'
> s = Readline.readline
now you can use the arrow keys
=> "now you can use the arrow keys"

Readline can take the prompt as an argument:

> s = Readline.readline("enter some text: ")
enter some text: here is some text
=> "here is some text"

Readline also has an optional second argument. This is a boolean which
specifies whether to keep history across readline method calls. The
default is false, but if you pass in true, you can use the up arrow to
retrieve input from previous readline calls:

> s = Readline.readline("enter some text: ", true)
enter some text: here is some text
=> "here is some text"
> s = Readline.readline("now press the up arrow: ", true)
now press the up arrow: here is some text
=> "here is some text"
Paul Gross

Paul Gross

I'm a lead software developer in Seattle working for Braintree Payments.

Read More