這是因爲自然月數是1
爲基礎的,而不是被0
爲基礎的像一個典型的陣列。爲了提供這一點,並避免必須記住在需要時執行索引計算,月份名稱數組剛好在zero
位置處使用額外元素定義。
看看date_helper code對於如何使用它的例子:
# Looks up month names by number (1-based):
#
# month_name(1) # => "January"
#
# If the <tt>:use_month_numbers</tt> option is passed:
#
# month_name(1) # => 1
#
# If the <tt>:use_two_month_numbers</tt> option is passed:
#
# month_name(1) # => '01'
#
# If the <tt>:add_month_numbers</tt> option is passed:
#
# month_name(1) # => "1 - January"
#
# If the <tt>:month_format_string</tt> option is passed:
#
# month_name(1) # => "January (01)"
#
# depending on the format string.
def month_name(number)
if @options[:use_month_numbers]
number
elsif @options[:use_two_digit_numbers]
'%02d' % number
elsif @options[:add_month_numbers]
"#{number} - #{month_names[number]}"
elsif format_string = @options[:month_format_string]
format_string % {number: number, name: month_names[number]}
else
month_names[number]
end
end