2012-12-31 68 views
-4

每次我在Ruby上打開它時,Ruby都會立即關閉,因爲它無法讀取代碼中的內容。所以在代碼中有一個錯誤,但我回顧了一切,似乎無法找到它。我需要一些幫助才能使這個代碼工作。非常感謝你提前!Ruby代碼不起作用。 Ruby無法讀取我的代碼。

Time.now=apply_time 
if apply_time.month <=3 
price=45 
elsif apply_time.month <=5 
price=55 
elsif apply_time.month <=7 
price=65 
else 
price=0 
end 
puts 'The fee to apply for the competition is $ ' + price.to_s + '.00 when you apply on the date of today, ' + Time.now.to_s +'.' 
puts 'If your fee came up as $0.00, then that is because the competition has ended. But do not worry, there is always next year!' 
Sleep 20 
+0

1)你不告訴你在哪個環境中。我記得在Windows XP下,如果運行ruby.exe(?),DOS框會立即關閉,如果運行rubyw.exe(?)或類似的東西,它會保持打開狀態。 2)你有和你之前的問題一樣的'puts'。 'Time.now.to_s +'。'':在Ruby +中是一種方法或一種加號,具體取決於你如何編寫它,有沒有空格。在IRB中鍵入它:「NoMethodError:undefined method'+ @'for」。「:String」表示解析器試圖爲字符串'。'計算一元正數,但在類String中沒有定義一元正數。 – BernardK

+0

查看http://stackoverflow.com/questions/14288118/ruby-when-i-try-to-launch-a-script-it-opens-and-then-immediately-closes – BernardK

回答

3

你的第一行是試圖分配apply_timeTime.now

嘗試一下週圍的其他方法:

apply_time = Time.now 

順便說一句,如果你從IRB運行它,你會看到錯誤:

NameError: undefined local variable or method `apply_time' for main:Object 

但即使您提前定義apply_time,您也會將ge t:

NoMethodError: undefined method `now=' for Time:Class 

因爲您不能在不粉碎Time類的情況下爲當前時間賦值。