如何在Ruby中实现不区分大小写的制表符完成?

时间:2019-02-18 18:42:12

标签: ruby

我正在使用HighLine::Question.readline在Ruby中实现tab completion。以下是tab_complete.rb的代码段,该代码段对我有用。

require "rubygems"
require "highline/import"

tab_complete = ["yes", "okay", "maybe", "no"]

selected = ask("Please say something: ", tab_complete) do |q|
  q.readline = true
end
p selected

-bash-4.1$ ruby tab_complete.rb
Please say something: yes
"yes"

上面的代码对于数组中的项目很好用,但是我想处理不区分大小写的制表符补全。例如,如果用户输入upcasedowncase y并按下tab键,则它应以downcase yes的形式完成,并且该值应为保存在selected中。现在,系统提示我输入区分大小写的单词,如下所示:

-bash-4.1$ ruby tab_complete.rb
Please say something: Y
You must choose one of [yes, okay, maybe, no].
?  Please say something:

任何有关此的指针将对继续前进非常有帮助。

1 个答案:

答案 0 :(得分:1)

它是开源的。

ask的块参数是具有case属性的Highline::Question的实例。这是the example的用法:

selected = ask("What: ", tab_complete) do |q|
  q.case = :down # THIS
  q.readline = true
end