57

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
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Kamilski81
  • 14,409
  • 33
  • 108
  • 161

3 Answers3

47

In IRB, include this line: require 'date' then you will be able to use DateTime.

irb(main):000:0> DateTime.class
NameError: uninitialized constant DateTime
        from (irb):0
        from /path/to/ruby/irb:12:in '(main)'
irb(main):001:0> require 'date'
=> true
irb(main):002:0> DateTime.class
=> Class
TekuConcept
  • 1,264
  • 1
  • 11
  • 21
4

Worked for me when first initializing with require 'date'.

Nomis
  • 437
  • 1
  • 4
  • 13
1

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'.

Kamilski81
  • 14,409
  • 33
  • 108
  • 161