本文簡要介紹ruby語言中 Readline.readline
的用法。
用法
readline(prompt = "", add_hist = false) → string or nil
顯示prompt
並通過行編輯讀取輸入的行。如果add_hist
為真,則輸入的行將添加到曆史記錄中。
當輸入的行為空且用戶輸入 EOF(在 UNIX 上按 ^D)時返回 nil。
如果滿足以下條件之一,則引發 IOError
異常。
-
標準輸入已關閉。
-
標準輸出已關閉。
此方法支持線程。等待輸入行時切換線程上下文。
輸入行時支持行編輯。提供 VI 和 Emacs 編輯模式。默認為 Emacs 編輯模式。
注意:在等待輸入行時,用戶按下“^C”後終止 ruby 解釋器並且不返回終端狀態。舉3個例子來避免它。
-
返回終端狀態後按 ^C 捕獲
Interrupt
異常: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
-
返回終端狀態後按 ^C 捕獲 INT 信號:
require "readline" stty_save = `stty -g`.chomp trap("INT") { system "stty", stty_save; exit } while buf = Readline.readline p buf end
-
忽略按 ^C:
require "readline" trap("INT", "SIG_IGN") while buf = Readline.readline p buf end
可以用 Readline::HISTORY
常數作如下。如果輸入的行為空或與上一行相同,則不記錄到曆史記錄中。
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
相關用法
- Ruby Readline.completion_proc =用法及代碼示例
- Ruby Readline.completion_case_fold用法及代碼示例
- Ruby Readline.point用法及代碼示例
- Ruby Readline.completion_append_character =用法及代碼示例
- Ruby Readline模塊用法及代碼示例
- Ruby Regexp named_captures()用法及代碼示例
- Ruby Recorder類用法及代碼示例
- Ruby Regexp to_s()用法及代碼示例
- Ruby Regexp.eql?用法及代碼示例
- Ruby Regexp hash()用法及代碼示例
- Ruby Resolution.new用法及代碼示例
- Ruby Regexp.fixed_encoding?用法及代碼示例
- Ruby Resolver.resolve用法及代碼示例
- Ruby Regexp類用法及代碼示例
- Ruby Regexp.options用法及代碼示例
- Ruby Refinement.import_methods用法及代碼示例
- Ruby Regexp inspect()用法及代碼示例
- Ruby Regexp.inspect用法及代碼示例
- Ruby Regexp.names用法及代碼示例
- Ruby Resolution.require_nested_dependencies_for用法及代碼示例
- Ruby Requirement.parse用法及代碼示例
- Ruby Regexp source()用法及代碼示例
- Ruby Resolution.possibility用法及代碼示例
- Ruby Regexp match()用法及代碼示例
- Ruby Regexp.rxp =~ str用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Readline.readline。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。