You’re coding some Ruby. You use gets.chomp to get user input. You run your ruby file. You type some text at the prompt. Looks good, right? But then you press the left arrow key and… ^[[D appears on the screen. WTF is that?

Arrow keys, like left arrow and right arrow, are control characters. The problem is that Ruby’s gets.chomp does not know how to listen for control characters. Instead of interpreting ^[[D as a left arrow, it thinks you just literally typed ^[[D.

The fix is simple. Don’t use gets.chomp, because gets.chomp doesn’t understand control characters. Use the readline library:

require 'readline'
Readline.readline()

That’s what irb uses, and that’s why you can use the left and right arrow keys in irb without seeing ^[[ garbage.