'因为大写,它们是常量。
我在学习 ruby 的 Date 助手时很开心。
1.9.3p125 :057 > Date::ABBR_MONTHNAMES
=> [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
1.9.3p125 :058 > Date::ABBR_DAYNAMES
=> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
1.9.3p125 :059 > Date::MONTHNAMES
=> [nil, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
1.9.3p125 :060 > Date::DAYNAMES
=> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
1.9.3p125 :070 > Date::MONTHNAMES[Time.new.month]
=> "August"
好玩的东西!但是 GREGORIAN、JULIAN、ENGLAND 和 ITALY (!) 常量呢?它们有什么用/我如何使用它们?我可以输出:
1.9.3p125 :061 > Date::GREGORIAN
=> -Infinity
1.9.3p125 :062 > Date::JULIAN
=> Infinity
1.9.3p125 :063 > Date::ENGLAND
=> 2361222
或者
1.9.3p125 :067 > Date.new
=> #<Date: -4712-01-01 ((0j,0s,0n),+0s,2299161j)>
1.9.3p125 :068 > Date.new.new_start(Date::JULIAN)
=> #<Date: -4712-01-01 ((0j,0s,0n),+0s,Infj)>
1.9.3p125 :069 > Date.new.new_start(Date::ENGLAND)
=> #<Date: -4712-01-01 ((0j,0s,0n),+0s,2361222j)>
从下面看起来,朱利安是一个差了几天的日历。我记得几个世纪前了解了日历重置,所以这是有道理的,但是英格兰和意大利以及它们将如何使用对我来说仍然不清楚。
1.9.3p125 :076 > Date.new(1977,7,1).new_start(Date::ENGLAND)
=> #<Date: 1977-07-01 ((2443326j,0s,0n),+0s,2361222j)>
1.9.3p125 :077 > Date.new(1977,7,1).new_start(Date::ITALY)
=> #<Date: 1977-07-01 ((2443326j,0s,0n),+0s,2299161j)>
1.9.3p125 :078 > Date.new(1977,7,1).new_start(Date::JULIAN)
=> #<Date: 1977-06-18 ((2443326j,0s,0n),+0s,Infj)>