2013-08-27 43 views
0

我在軌道上比較紅寶石日期時陷入了奇怪的問題。在軌道上比較紅寶石日期

這是我想要做的。我已經解析了一個文件並選擇了一個日期到一個變量@timeinFile。我正在將此值與我的Usercreated_at進行比較。這裏是代碼:

@date=User.last.created_at 
@arr.each do |i| 
    j=i.partition('(')[2] 
    @timeinFile=j.partition(')')[0].to_datetime 
    if @date < @timeinFile #Error: undefined method `to_datetime' for nil:NilClass 
      @val='true' 
    end 
end 

if語句和粘貼值分別我得到下面的輸出

<p> <%= @date %> </p> ----------> 2013-08-05 09:16:33 UTC 

<p><%= @timeinFile.to_time %></p> -------------> 2012-10-02 12:00:00 UTC 

PLZ有人告訴我,我做了什麼錯了,當我的評論。

編輯

<a href="http://www.cpsc.gov/en/Recalls/2013/Fitness-Anywhere-Recalls-Early-Model-Suspension-Trainer-Devices-Due-to-Fall-Hazard/" target="_blank">Fitness Anywhere Recalls Early Model Suspension Trainer Devices Due to Fall Hazard</a> (Tue, 02 Oct 2012 12:00:00 GMT) Fitness Anywhere has received 570 reports of the strap length adjustment buckles breaking with 13 reports of injuries. TRX suspension trainers sold from 2006 through 2009. </p> </body> </html> 

這是我在@arr讓行。下一步是與(,然後用)分區得到圓括號日期...

+0

您確定錯誤指向比較嗎? –

+1

當出現錯誤時,查看「@ arr」的內容會很有用。 –

+0

@MarekLipka是的,它指向同一行:( –

回答

3

那麼,爲什麼你在代碼中使用@timeinFile@timeonsite在你的測試?

#Error: undefined method `to_datetime' for nil:NilClass 

基本上就意味着你是一個nil對象上調用一個方法,我覺得錯誤觸發上線前的if聲明。

當你把這張支票放進去時會發生什麼?

unless j.partition(')')[0].nil? 
    @timeinFile=j.partition(')')[0].to_datetime 
else 
    @timeinFile = DateTime.now 
end 

如果它不給你一個錯誤,你可以去找出爲什麼這個值是nil

+0

是的,感謝編輯@Mischa,今天早上我的打字很搖晃。 – Miotsu