Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby NameError when referring to Date

Tags:

scripting

ruby

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 global namespace? Or am I doing something else wrong here?

like image 398
Parker Avatar asked Jun 11 '10 14:06

Parker


1 Answers

Try require 'date'.

like image 157
wdebeaum Avatar answered Nov 14 '22 06:11

wdebeaum