2010-06-11 166 views
4

我得到「未初始化的常量日期(NameError)」用下面的代碼:紅寶石NameError時日期

class Test 
    attr_accessor :reqs 

    def initialize() 
    @reqs = [] 
    end 
end 


class TestBuilder 

    def test(&block) 
    @current = Test.new 
    block.call 
    @current 
    end 

    def older_than_days(age) 
    @current.reqs << lambda { |email| ::Date.parse(email[:date]) < ::Date.today - age } 
    end 

end 


b = TestBuilder.new 
x = b.test { b.older_than_days(1) } 

p x.reqs[0].call({:date => "Mon, 5 Apr 2010 03:17:46 -0400"}) 

加入雙冒號閱讀回答這個問題後: Uninitialized constant ... NameError因爲ruby試圖在TestBuilder中找到Date。 Date是否在全局命名空間中?或者我在這裏做錯了什麼?

回答