Bug #6702

Date should be either required or not

Added by rosenfeld (Rodrigo Rosenfeld Rosas) over 8 years ago. Updated over 1 year 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.


Updated by mame (Yusuke Endoh) over 8 years ago

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


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

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 8 years ago

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

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 8 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 ;)

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 8 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.

Updated by drbrain (Eric Hodel) over 8 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.

Updated by nobu (Nobuyoshi Nakada) over 8 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.

Updated by kommen (Dieter Komendera) almost 8 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?

Updated by mame (Yusuke Endoh) almost 8 years ago

  • Status changed from Closed to Assigned

Eric, what do you think?

Yusuke Endoh

Updated by drbrain (Eric Hodel) almost 8 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.

Updated by drbrain (Eric Hodel) almost 8 years ago

  • Target version changed from 1.9.3 to 2.6

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.

Updated by kommen (Dieter Komendera) over 7 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.


Updated by naruse (Yui NARUSE) almost 3 years ago

  • Target version deleted (2.6)

Updated by hsbt (Hiroshi SHIBATA) over 1 year ago

  • Assignee changed from drbrain (Eric Hodel) to hsbt (Hiroshi SHIBATA)
  • Status changed from Assigned to Rejected

This issue was too old. If anyone still has an issue, Please report the new issue.

Also available in: Atom PDF