我正在使用以下代码获得“未初始化的常量日期(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不在全局命名空间中吗?或者我在这里做错了什么?
答案 0 :(得分:10)
尝试require 'date'
。