Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow keys don't work in irb/ruby command line scripts?

Tags:

scripting

ruby

While using gets to accept user input, pressing the arrow keys outputs text to the screen, presumably the character codes. How can I prevent this from happening, and further how can I get the arrow keys to properly move the cursor around?

irb(main):001:0> foo = gets
^[[A^[[D^[[B^[[C    
=> "\e[A\e[D\e[B\e[C\n"

Edit: maybe I should mention I'm using Mac OS X and Terminal.app.

like image 579
enure Avatar asked Feb 27 '23 16:02

enure


1 Answers

Using Ruby's Readline Library:

require 'readline'
foo = Readline::readline
like image 105
Josh Lee Avatar answered Mar 06 '23 15:03

Josh Lee