【发布时间】:2011-05-17 20:26:32
【问题描述】:
我有一个简短的脚本,它使用正则表达式在文件中搜索用户键入的特定短语。基本上,它是一个简单的搜索框。
我现在正试图让这个搜索框有一个 GUI,这样用户就可以在一个框中输入内容,并让他们的匹配“提醒”他们。
我不熟悉如何详细地使用红宝石鞋,并且一直在使用 TheShoeBox 网站上的示例。
谁能指出我的代码哪里出错了?
这是我的命令行版本:
string = File.read('db.txt')
puts "Enter what you're looking for below"
begin
while(true)
break if string.empty?
print "Search> "; STDOUT.flush; phrase = gets.chop
break if phrase.empty?
names = string.split(/\n/)
matches = names.select { |name| name[/#{phrase}/i] }
puts "\n \n"
puts matches
puts "\n \n"
end
end
这是我在 Ruby Shoes 中使用它的尝试:
Shoes.app :title => "Search v0.1", :width => 300, :height => 150 do
string = File.read('db.txt')
names = string.split(/\n/)
matches = names.select { |name| name[/#{phrase}/i] }
def search(text)
text.tr! "A-Za-z", "N-ZA-Mn-za-m"
end
@usage = <<USAGE
Search - This will search for the inputted text within the database
USAGE
stack :margin => 10 do
para @usage
@input = edit_box :width => 200
end
flow :margin => 10 do
button('Search') { @output.matches }
end
stack(:margin => 0) { @output = para }
end
非常感谢
【问题讨论】:
-
所以,只要确保这段代码一切正常。例如,
phrase从未声明,但您在此代码中使用它。