0

我正在编写一个需要链接外部脚本的程序。

目前我正在尝试编写一个验证器方法,该方法检查给定目录中是否存在外部脚本;如果没有,则要求最终用户输入包含脚本的目录的完整路径或相对路径。

但是,我想让用户选择退出而不进入目录。

这是我目前正在使用的方法。如果取消注释注释部分,即使 inp.downcase == "quit"... 仍然会引发参数错误

def signalp_validator(signalp_dir)
    if File.exist? "#{signalp_dir}/signalp"
        signalp_directory = signalp_dir
    else
        puts # a blank line
        puts "Error: The Signal P directory cannot be found in the following location: \"#{signalp_dir}/signalp\"."
        begin 
            puts # a blank line
            puts "Please enter the full path or a relative path to the Signal P directory." 
            print "> "
            inp = $stdin.gets.chomp
        raise ArgumentError, "Error: The Signal P directory cannot be found in the following location: \"#{inp}/signalp\"." unless File.exist? "#{inp}/signalp" # || inp.downcase == "quit"
        rescue Exception => e
            puts # a blank line
            puts e.message
        retry
        else
        #   if inp.downcase == "quit"
        #       abort "\nError: A output directory is required - please create one and then try again.\n\n"
        #   else
                signalp_directory = inp
            end
        end
    end
    return signalp_directory 
end

如果我从此更改 RaiseArgument 行(如上面的脚本中所示)

raise ArgumentError, "Error: The Signal P directory cannot be found in the following location: \"#{inp}/signalp\"." unless File.exist? "#{inp}/signalp" || inp.downcase == "quit" 

对此,

raise ArgumentError, "Error: The Signal P directory cannot be found in the following location: \"#{inp}/signalp\"." unless inp.downcase == "quit" || File.exist? "#{inp}/signalp"

我收到以下错误

    project/np_search/lib/np_search/library.rb:17: syntax error, unexpected tSTRING_BEG, expecting keyword_end (SyntaxError)
    ...case == "quit" || File.exist? "#{inp}/signalp"
    ...                               ^

有谁知道我做错了什么以及如何解决。

非常感激任何的帮助。

4

2 回答 2

1

你的线路

unless inp.downcase == "quit" || File.exist? "#{inp}/signalp"

被解释为

unless inp.downcase == ("quit" || File.exist?) "#{inp}/signalp"

这是无效的。为避免这种情况,请执行

unless (inp.downcase == "quit") || File.exist?("#{inp}/signalp")

或者

unless inp.downcase == "quit" or File.exist? "#{inp}/signalp"
于 2013-10-14T15:51:06.853 回答
1

我认为@sawa 的回答很好地指出了这一点。

另一件事是停止对控制流使用异常。

您可以使用looporwhile进行此输入检查。在此处查看这些语言结构:http ://www.tutorialspoint.com/ruby/ruby_loops.htm或此处http://ruby.bastardsbook.com/chapters/loops/

于 2013-10-14T16:00:54.660 回答