我有一个包含数字 1-31 的大字符串。我怎样才能将月份的名称居中?
My code:
class Month
attr_reader :month, :year
def initialize( month, year)
@month = month
@year = year
end
def month_names
names_of_months = {1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'}
return names_of_months[@month]
end
def length
days_of_months = {1 => 31, 2 => 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31}
return days_of_months[@month]
end
def to_s
output = "#{month_names} #{year}\nSu Mo Tu We Th Fr Sa\n"
(1..length).each do |day|
output << day.to_s
end
output
end
end
终端状态:
Failure:
TestMonth#test_to_s_on_jan_2017 [test/test_month.rb:39]
Minitest::Assertion: --- expected
+++ actual
@@ -1,9 +1,3 @@
-"January 2017
+"January 2017
Su Mo Tu We Th Fr Sa
- 1 2 3 4 5 6 7
- 8 9 10 11 12 13 14
-15 16 17 18 19 20 21
-22 23 24 25 26 27 28
-29 30 31
-
-"
+12345678910111213141516171819202122232425262728293031"