开发者

Ruby NameError when referring to Date

开发者 https://www.devze.com 2023-01-02 23:16 出处:网络
I\'m getting \"uninitialized constant Date (NameError)\" with the following code: class Test attr_accessor :reqs

I'm getting "uninitialized constant Date (NameError)" with the following code:

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"} )

The double-colon was added after reading the answer to this problem: Uninitialized constant ... NameError because ruby was trying to find Date in TestBuilder. Is Date not in the glo开发者_如何学运维bal namespace? Or am I doing something else wrong here?


Try require 'date'.

0

精彩评论

暂无评论...
验证码 换一张
取 消