带 Ruby Shoes 的搜索框

发布于 2024-11-07 20:36:45 字数 1220 浏览 4 评论 0原文

我有一个简短的脚本,它使用正则表达式在文件中搜索用户输入的特定短语。基本上,它是一个简单的搜索框。

我现在正在尝试使这个搜索框具有 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

非常感谢

I have a short script that uses regular expressions to search a file for a specific phrase that a user types in. Basically, it's a simple search box.

I'm now trying to make this search box have a GUI, so that users are able to type into a box, and have their matches 'alerted' to them.

I'm new to using ruby shoes in any great detail, and have been using the examples on TheShoeBox website.

Can anyone point out where I'm going wrong with my code?

Here is my command line version that works:

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

Here is my attempt at using it within 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

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本宫微胖 2024-11-14 20:36:45

好吧,对于初学者来说,第一个代码位可以整理一下。

file = File.open 'db.txt', 'rb'
puts "Enter (regex) search term or quit:"

exit 1 unless file.size > 0
loop do
  puts
  print "query> "
  redo if ( query = gets.chomp ).empty?
  exit 0 if query == "quit"
  file.each_line do |line|
    puts "#{file.lineno}: #{line}" if line =~ /#{query}/i
  end
  file.rewind
end

rb 选项让它在 Windows 中按预期工作(特别是对于 Shoes,您应该尝试与平台无关)。例如, chomp 会剥离 \r\n\n 但不会剥离 a,而 chop< /code> 只是盲目地去掉最后一个字符。 loop do endwhile true 更好。另外为什么将匹配项存储在变量中?只需逐行读取文件(允许 CRLF 结尾),而不是按 \n 分割,尽管剩余的 \r 不会真正造成太大问题...

至于 Shoes 位:

Shoes.app :title => "Search v0.2", :width => 500, :height => 600 do

  @file = File.open 'db.txt', 'rb'

  def search( file, query )
    file.rewind
    file.select {|line| line =~ /#{query}/i }.map {|match| match.chomp }
  end

  stack :margin => 10 do
    @input  = edit_line :width => 400

    button "search" do
      matches = search( @file, @input.text )
      @output.clear
      @output.append do
        matches.empty? ?
          title( "Nothing found :(" ) :
          title( "Results\n" )
      end
      matches.each do |match|
        @output.append { para match }
      end
    end

    @output = stack { title "Search for something." }

  end

end

您从未定义过 @output.matches 或调用过 search() 方法。看看现在是否有意义。

Well, for starters, the first code bit can be neatened up.

file = File.open 'db.txt', 'rb'
puts "Enter (regex) search term or quit:"

exit 1 unless file.size > 0
loop do
  puts
  print "query> "
  redo if ( query = gets.chomp ).empty?
  exit 0 if query == "quit"
  file.each_line do |line|
    puts "#{file.lineno}: #{line}" if line =~ /#{query}/i
  end
  file.rewind
end

The rb option lets it work as expected in Windows (especially with Shoes, you should try and be platform-independent). chomp strips off \r\n and \n but not a for example, while chop just blindly takes off the last character. loop do end is nicer than while true. Also why store matches in a variable? Just read through the file line by line (which allows for CRLF endings) as opposed to splitting by \n although the residual \r wouldn't really pose much of a problem...

As for the Shoes bit:

Shoes.app :title => "Search v0.2", :width => 500, :height => 600 do

  @file = File.open 'db.txt', 'rb'

  def search( file, query )
    file.rewind
    file.select {|line| line =~ /#{query}/i }.map {|match| match.chomp }
  end

  stack :margin => 10 do
    @input  = edit_line :width => 400

    button "search" do
      matches = search( @file, @input.text )
      @output.clear
      @output.append do
        matches.empty? ?
          title( "Nothing found :(" ) :
          title( "Results\n" )
      end
      matches.each do |match|
        @output.append { para match }
      end
    end

    @output = stack { title "Search for something." }

  end

end

You never defined @output.matches or called your search() method. See if it makes sense now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文