Kamilski81 Kamilski81 - 7 months ago 40
Ruby Question

Why is Ruby's Date class automatically loaded but DateTime is not?

Using IRB, why are the Date & Time classes automatically loaded, but DateTime is not? I have to

require 'date'
, this does not make sense to me because I thought that both Date and DateTime were using the standard library
'date'
?

ruby-1.9.2-p290 :001 > Date
=> Date
ruby-1.9.2-p290 :002 > Time
=> Time
ruby-1.9.2-p290 :003 > DateTime
NameError: uninitialized constant Object::DateTime
from (irb):3
from /Users/kamilski81/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :004 > require 'date'
=> true
ruby-1.9.2-p290 :005 > require 'date'
=> false
ruby-1.9.2-p290 :006 > DateTime
=> DateTime

Answer

Being a little more curious, I tried:

$ ruby -e 'puts DateTime.class'
-e:1:in `<main>': uninitialized constant Object::DateTime (NameError)
[~, kamilski81@mac]
$ ruby -e 'puts Date.class'
-e:1:in `<main>': uninitialized constant Object::Date (NameError)
$ ruby -e 'puts Time.class'
Class

So it makes me think that it's an irb issue that automatically loads 'date'.