2015-07-19 51 views
-1

我有代碼:紅寶石意想不到的關鍵字結束

results = ["http://www.google.com", "http://www.yahoo.com.uk"] 

results.each do |results| 
    File.open("#{results}.html", "w") do |file| 
    file.write(RestClient.get(#{result})) 
    end 
end 

當我運行程序時,它給了我一個錯誤:

syntax error, unexpected keyword_end, expecting ')' 

我有兩個do -s和兩個end -s。請幫忙。

+0

如果將它放在一行中,那會怎麼樣:'File.open(「#{results} .html」,「w」){| file | file.write(RestClient.get(#{result}))}'?它會拋出一個錯誤嗎? –

+0

請發佈完整的錯誤消息和相應的行號。此外,似乎應該檢查何時使用「結果」和何時使用「結果」。 – spickermann

+0

是的!此外,我還有另一個「做」。嘗試使用兩個{},仍然是一個錯誤。 –

回答

6

#用於串插,但只在一個適當的string literal

字符串
File.open("#{results}.html", "w") 
#   ^^^^^^^^^^ 
#   string interpolation 

外,#開始comment

file.write(RestClient.get(#{result})) 
#       ^^^^^^^^^^^ 
#       comment 

你的編輯器應該能夠相應地突出顯示:

vim screenshot

「修理」 它,使用:

file.write(RestClient.get(result)) 

順便說一句,這裏還有在你的代碼中的另一個錯字:

results.each do |results| 

應該是:

results.each do |result| 

result則是指您的results陣列中的單個元素(仔細檢查您的代碼resultresults)。

+0

謝謝!但是使用'fix'給出: 4:在'initialize'中:沒有這樣的文件或目錄@ rb_sysopen - http://www.google.com.html(Errno :: ENOENT) \t from trytrytry.rb:4:在'新 '從trytrytry.rb \t:在

在'塊:4 '從trytrytry.rb \t:3:'每個' \t從trytrytry.rb:3:在'
' –

+0

曾經也校正的「結果「和」結果「。謝謝! –

+0

新代碼: results = [「http://www.google.com」,「http://www.yahoo.com.uk」] results.each do | result | file = File.new(「#{result} .html」,「w」)do | file | file.write(RestClient.get(結果)) file.close 結束 結束 仍然不起作用 –