Why does a date exist in Ruby before it is needed?

In Ruby, I expect a class that was not needed to raise an "uninitialized constant" error. For example, this applies to CSV .

However, Date behaves strangely: it is available, but does not seem to work until it is needed.

 ~: irb >> Date.new(2012,7,24) ArgumentError: wrong number of arguments(3 for 0) >> require 'date' => true >> Date.new(2012,7,24) => #<Date: 2012-07-24 ((2456133j,0s,0n),+0s,2299161j)> 

What explains this behavior?

+23
date ruby
Jul 24. 2018-12-12T00:
source share
3 answers

I believe that date does not come from irb , but from rubygems , namely the file where Gem::Specification defined:

 class Date; end # for ruby_code if date.rb wasn't required 

I believe that they need some date class so that the interpreter does not complain further in the Specification class.

+10
Jul 24. 2018-12-12T00:
source share

Like this question . irb loads the Date class by default, but Ruby itself does not (try, for example, puts Date.new in a file).

It seems that the Date class that irb loads is different from the distribution class as you pointed out. Moreover, this is similar to Ruby 1.9 - if I try in 1.8, I get the same class methods before and after the requirement.

+5
Jul 24 2018-12-12T00:
source share

Partial answer: it seems that the incomplete Date class comes from irb, not from ruby.

+3
Jul 24 2018-12-12T00:
source share



All Articles