Bug #6702

Date should be either required or not

Added by rosenfeld (Rodrigo Rosenfeld Rosas) almost 5 years ago. Updated over 3 years ago.

Target version:
ruby -v:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


Today I was trying to do some experiment with IRB: "" threw a NoMethodError.

But Date was defined: "defined? Date" == "constant".

If I explicitly required for "date" it worked, but this is pretty much weird. It would be better if either:

  • I got an undefined "Date" class instead
  • "date" was automatically required

The current state is pretty much misleading.

This also happens in JRuby, so I guess somehow this is an intended behavior, but it doesn't make any sense to me.


#1 Updated by mame (Yusuke Endoh) almost 5 years ago

  • Assignee set to drbrain (Eric Hodel)
  • Status changed from Open to Assigned


This is caused by rubygems/specification:

$ ruby -rrubygems/specification -e 'p Date;'
-e:1:in <main>': undefined methodtoday' for Date:Class (NoMethodError)


Eric, is this intentional?

Yusuke Endoh

#3 [ruby-core:46203] Updated by rosenfeld (Rodrigo Rosenfeld Rosas) almost 5 years ago

By the way, what is the reason for Time being included by default but not Date?

#4 [ruby-core:46208] Updated by rosenfeld (Rodrigo Rosenfeld Rosas) almost 5 years ago

Is Date that slow to require when compared to Time? Wouldn't it be possible to make it faster to load by lazy loading some parts and include it by default instead of having to require it manually?

That would certainly make programmers happier ;)

#5 [ruby-core:46209] Updated by rosenfeld (Rodrigo Rosenfeld Rosas) almost 5 years ago

By the way, I forgot to say that that pull request was rejected. Please take a look at the link above for the reasoning behind it.

#6 [ruby-core:46212] Updated by drbrain (Eric Hodel) almost 5 years ago

  • Status changed from Assigned to Closed

The pull request was rejected due to its implementation. For compatibility with old gems a Date class must be defined. I decided to switch to require 'date' since rubygems/specification.rb is lazily loaded now and the cost of loading it is low. The commit to rubygems will be imported in the future so I will close this ticket.

Time is part of the core libraries with parsing and extra output formats defined in time.rb. Date exists entirely outside of the core ruby classes so you must require it separately.

Historically Date was much slower than Time due to its implementation. Today that gap is narrower, but Date is still slower than Time. (As a trade-off, Date and DateTime give you a much larger range than a Time.)

Remember, you must require libraries you directly depend on. Do not expect a dependency to load it for you indirectly. This will only lead to bugs and incompatibility as your dependencies change.

#7 [ruby-core:46213] Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

drbrain (Eric Hodel) wrote:

(As a trade-off, Date and DateTime give you a much larger range than a Time.)

"Date and DateTime had given ..."

In 1.9 or later, Time has arbitrary range and precision.

#8 [ruby-core:50570] Updated by kommen (Dieter Komendera) over 4 years ago

This is an issue again in Ruby 2.0.0 preview 2 as the require 'date' in rubygems/specification.rb as described in note 6 in this issue was removed again with this commit:

I found it to be a backward compatibility issue for example with the compass gem:

Should this issue be reopened?

#9 [ruby-core:50572] Updated by mame (Yusuke Endoh) over 4 years ago

  • Status changed from Closed to Assigned

Eric, what do you think?

Yusuke Endoh

#10 [ruby-core:50588] Updated by drbrain (Eric Hodel) over 4 years ago

I don't know if I can solve this problem with RubyGems.

Date is now a C extension.

At ruby install time, Gem::Specification is loaded by rbinstall.rb which is run from miniruby.

miniruby cannot load C extensions (as mentioned in the commit referenced above).

I'll see if Date can be required only when needed. I believe rbinstall calls ruby_code which uses Date, so it may be difficult.

#11 [ruby-core:50958] Updated by drbrain (Eric Hodel) over 4 years ago

  • Target version changed from 1.9.3 to next minor

I am not clever enough to solve this problem.

As I said before, tool/rbinstall.rb cannot load extensions. If I try to lazy-load Date in lib/rubygems/specification.rb tool/rbinstall.rb obviously, tool/rbinstall.rb fails.

I cannot figure out how to only load Date when RubyGems was not loaded from mini ruby. Attempting to detect the LoadError results in an infinite loop.

Perhaps someone more clever than I can fix this. I will leave it open for now.

#12 [ruby-core:56829] Updated by kommen (Dieter Komendera) over 3 years ago

Just a short update from my side: Since my issue from comment #8 was mostly caused by bad .gemspec files from gems which didn't require 'date' before using it and most (all I know) have been fixed, I think this is not an issue any longer.

Though I still a agree with the original bug report, that this is a strange behaviour.

Also available in: Atom PDF