Bug #6702

Date should be either required or not

Added by Rodrigo Rosenfeld Rosas almost 2 years ago. Updated 8 months ago.

[ruby-core:46200]
Status:Assigned
Priority:Low
Assignee:Eric Hodel
Category:lib
Target version:next minor
ruby -v:ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] Backport:

Description

Today I was trying to do some experiment with IRB: "Date.today" 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.

History

#1 Updated by Yusuke Endoh almost 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Eric Hodel

Hello,

This is caused by rubygems/specification:

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

See: https://github.com/rubygems/rubygems/blob/62653cb808b7020eec98151445f4caf0812b01ce/lib/rubygems/specification.rb#L47

Eric, is this intentional?

Yusuke Endoh mame@tsg.ne.jp

#3 Updated by Rodrigo Rosenfeld Rosas almost 2 years ago

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

#4 Updated by Rodrigo Rosenfeld Rosas almost 2 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 Updated by Rodrigo Rosenfeld Rosas almost 2 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 Updated by Eric Hodel almost 2 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 Updated by Nobuyoshi Nakada almost 2 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 Updated by Dieter Komendera over 1 year 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: https://github.com/rubygems/rubygems/commit/04422c3c7fc0273a5ef9d01641fb0a2a4ee0d03d

I found it to be a backward compatibility issue for example with the compass gem: https://github.com/chriseppstein/compass/blob/stable/compass.gemspec#L6

Should this issue be reopened?

#9 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Closed to Assigned

Eric, what do you think?

Yusuke Endoh mame@tsg.ne.jp

#10 Updated by Eric Hodel over 1 year 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 Updated by Eric Hodel over 1 year 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 Updated by Dieter Komendera 8 months 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