readline
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378 (0)
- 1_9_2_180 (0)
- 1_9_3_125 (0)
- 1_9_3_392 (0)
- 2_1_10 (-38)
- 2_2_9 (0)
- 2_4_6 (0)
- 2_5_5 (0)
- 2_6_3 (0)
- What's this?
readline(p1 = v1, p2 = v2)
public
Shows the prompt and reads the inputted line with line editing. The inputted line is added to the history if add_hist is true.
Returns nil when the inputted line is empty and user inputs EOF (Presses ^D on UNIX).
Raises IOError exception if one of below conditions are satisfied.
-
stdin was closed.
-
stdout was closed.
This method supports thread. Switches the thread context when waits inputting line.
Supports line edit when inputs line. Provides VI and Emacs editing mode. Default is Emacs editing mode.
NOTE: Terminates ruby interpreter and does not return the terminal status after user pressed ā^Cā when wait inputting line. Give 3 examples that avoid it.
-
Catches the Interrupt exception by pressed ^C after returns terminal status:
require "readline" stty_save = `stty -g`.chomp begin while buf = Readline.readline p buf end rescue Interrupt system("stty", stty_save) exit end end end
-
Catches the INT signal by pressed ^C after returns terminal status:
require "readline" stty_save = `stty -g`.chomp trap("INT") { system "stty", stty_save; exit } while buf = Readline.readline p buf end
-
Ignores pressing ^C:
require "readline" trap("INT", "SIG_IGN") while buf = Readline.readline p buf end
Can make as follows with Readline::HISTORY constant. It does not record to the history if the inputted line is empty or the same it as last one.
require "readline" while buf = Readline.readline("> ", true) # p Readline::HISTORY.to_a Readline::HISTORY.pop if /^\s*$/ =~ buf begin if Readline::HISTORY[Readline::HISTORY.length-2] == buf Readline::HISTORY.pop end rescue IndexError end # p Readline::HISTORY.to_a print "-> ", buf, "\n" end