0

ruby-v2.2.3 is supposed to have Date class preloaded into irb, however when I enter...

Date NameError: uninitialized constant Date from (irb):1 from /Users/noah/.rubies/ruby-2.2.3/bin/irb:11:in `'

Why should I have to require Date every single time if it's supposed to be preloaded into 2.2.3?

n.milsht
  • 163
  • 3
  • 17

4 Answers4

3

Date isn't listed as a core class in v2.2.3 or the current Ruby v2.3.1 core-classes, but Time is. Here's some IRb output:

$ irb -f
irb(main):001:0> Date.class
NameError: uninitialized constant Date
Did you mean?  Data
  from (irb):1
  from /Users/ttm/.rbenv/versions/2.3.1/bin/irb:11:in `<main>'
irb(main):002:0> Time.class
=> Class
irb(main):003:0> Time.methods(false)
=> [:at, :now, :utc, :gm, :local, :mktime]

That is a limited subset of Time's methods though:

irb(main):002:0> require 'time'
=> true
irb(main):003:0> Time.methods(false)
=> [:at, :now, :utc, :gm, :local, :mktime, :parse, :zone_offset, :strptime, :rfc2822, :rfc822, :httpdate, :xmlschema, :iso8601]
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0

Why do you say Date is preloaded? It's not a core class, it's part of the stdlib so it needs to be required. Time is a core class instead.

Ursus
  • 29,643
  • 3
  • 33
  • 50
0

You can try to do the following in the beginning of your file:

require 'Date'
Tilek
  • 636
  • 1
  • 8
  • 14
  • So upper case `Date` gave me an error, that's why I posted [this separate answer](https://stackoverflow.com/a/74532788/4575793). I hope, that's fine with you. – Cadoiz Nov 22 '22 at 12:41
0

As far as I know, (and as Ursus said), Date is not preloaded.

Attention: So Tilec suggested to just load the library at the beginning of the file, which gave me (Not sure if that is true in general): this gave me LoadError: cannot load such file -- Date. Trying gem install Date gives me

ERROR:  Could not find a valid gem 'Date' (>= 0) in any repository
ERROR:  Possible alternatives: date

Solution: So I'm proposing to correct to lower case:

require 'date'
Cadoiz
  • 1,446
  • 21
  • 31