2014-03-04 27 views
0
currDir = "" 
# 
# regex is from stack overflow question: 
dirRegex = Regexp.new '^(?!.*[\\/]\.{2}[\\/])(?!\.{2}[\\/])[-\w.\\/]+$' 
if ARGV.length == 1 && $1.to_s.match dirRegex 
    currDir = $1 
    puts $1 
    puts "#{currDir}" 
    puts ARGV.length 
else 
    currDir = "./" 
    puts $1 
    puts "#{currDir}" 
    puts ARGV.length 
end 

當我試圖讓上面的代碼匹配一個目錄如home或〜/ test /它會給我一個錯誤。我如何使這個匹配給出的目錄

./script.rb /home/local/NKU/dixonc3/test 
./script.rb:9: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n' 
./script.rb:14: syntax error, unexpected keyword_else, expecting $end 
+1

您可以通過編寫這樣..'ARGV.length ==還修復1 &&($ 1.to_s.match dirRegex) '..由於更高的優先級'&&','ARGV.length == 1 && $ 1.to_s.match dirRegex'被視爲'(ARGV.length == 1 && $ 1.to_s.match)dirRegex' ..這裏它提高了* BooM! BooM !!!! * :-) –

+0

儘管我的參數仍然有問題。讓我知道這是否需要一個單獨的問題。如果我只是通過它我的主目錄〜它說「不能轉換零字符串」 – camdixon

+0

是的..單獨的問題..通過接受這個答案... –

回答

5

變化:

if ARGV.length == 1 && $1.to_s.match dirRegex # line 5 

到:

if ARGV.length == 1 && $1.to_s.match(dirRegex) 
+1

是的.. +1 ...這是答案 –

+1

@Agis我不敢相信這很簡單。 :( – camdixon

相關問題