Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

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
like image 371
Kamilski81 Avatar asked Mar 14 '12 15:03

Kamilski81


3 Answers

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
like image 123
TekuConcept Avatar answered Oct 16 '22 18:10

TekuConcept


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

like image 4
Nomis Avatar answered Oct 16 '22 19:10

Nomis


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

like image 1
Kamilski81 Avatar answered Oct 16 '22 19:10

Kamilski81