0

I am writing a program that requires an external script to be linked.

At the moment I am trying to write a validator method, that checks if the external script is present in the given directory; and if not, then the end-user is asked to input the full or relative path to the directory containing the script.

However, I want to give the user the option to exit and not enter the directory.

Here is the method that I am currently using. If uncomment the commented parts, the argument error is still raised even if 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

If I change the RaiseArgument line from this (as is in the above script)

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

to this,

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

I get the following error

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

Does anyone have idea what I am doing wrong and how it could be fixed.

Any help would be most appreciated.

2 Answers 2

1

Your line

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

is being interpreted as

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

which is invalid. To avoid that, do

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

or

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

i think the answer of @sawa points it out quite well.

another thing would be to stop using exceptions for your control flow.

you could use loop or while for this input checking. have a look at those language constructs here: http://www.tutorialspoint.com/ruby/ruby_loops.htm or here http://ruby.bastardsbook.com/chapters/loops/

2
  • Thanks, just wondering whether there is a reason why I shouldn't use exceptions in control flow (the retry method I used can be found right at the bottom of the first link you kindly provided). Is this simply seen as bad practice? Secondly, in a previous question, someone also told me to get rid of the argument errors (in a different situation), so when is it 'good practice' to use them... Oct 15, 2013 at 10:00
  • 1
    yes, using exceptions for control-flow is considered a bad practice. another thing is that it's hard to test and also expensive of in terms of creating exception objects. argument errors are fine if you create some kind of API or library where you want tell something to a consumer of the library. in other words, use exceptions for exceptional cases, having a user to retry something is not an exception, its a common usecase.
    – phoet
    Oct 15, 2013 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.