在 Ruby 中,我预计不需要的类会引发“未初始化常量”错误。情况就是这样CSV
, 例如。
然而,Date
行为很奇怪:它可用,但显然不起作用,直到需要它为止。
~: 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)>
如何解释这种行为?
我相信date
不是来自irb
,但是从rubygems
, 具体来说the file https://github.com/ruby/ruby/blob/ruby_1_9_3/lib/rubygems/specification.rb#L13 where Gem::Specification
被定义为:
class Date; end # for ruby_code if date.rb wasn't required
我相信他们需要any Date
定义类,以便解释器不会在下面进一步抱怨Specification
class.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)