我可以使用什么方法或 class in Ruby 在每个日期之间添加空格?
What method or class in Ruby can i use to add spaces between each date?
我如何移动每个 date/number 或添加空格以便数字可以放在相应的 Day 下?我可以使用 class 或 ruby 中的方法吗?顺便说一句,我不能使用 ruby 中的日期。
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
weekdays = "Su Mo Tu We Th Fr Sa" <<"\n"
month = "#{month_names} #{year}"
output =[
month.center(weekdays.size),
weekdays
].join("\n")
(1..length).each_slice(7) do |week|
output << week.join
output << "\n"
end
output
end
end
这是我从终端获得的结果:
Failure:
TestMonth#test_to_s_on_jan_2017 [test/test_month.rb:39]
Minitest::Assertion: --- expected
+++ actual
@@ -1,9 +1,8 @@
-" 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
-
+1234567
+891011121314
+15161718192021
+22232425262728
+293031
"
4 tests, 5 assertions, 3 failures, 0 errors, 0 skips
查看 String#rjust
和附带的示例。它会让您向字符串添加填充。
以下应将您想要的间距添加到一周中的每一天:
(1..length).each_slice(7) do |week|
output << week.map { |day| day.to_s.rjust(3) }.join
output << "\n"
end
但是查看您的预期输出,您想要删除每行的第一个 space。为此,试试这个:
(1..length).each_slice(7) do |week|
output << week.map { |day| day.to_s.rjust(3) }.join[1..-1]
output << "\n"
end
注意 [1..-1]
部分,这将删除每个 week
字符串的第一个字符。
旁注
附带说明一下,您不需要在方法中显式 return
或 class 变量,因为您已将它们定义为 attr_reader
.
此外,为了稍微清理一下,您甚至可以将 month_names
方法重构为类似于下面的代码(请注意,我已将方法名称更改为 month_name
)。 length
甚至 to_s
也可以这样做,但这可能是个人品味的问题。
def month_name
month_names[month - 1]
end
private
def month_names
%w(January February March April May June July August September October November December)
end
我如何移动每个 date/number 或添加空格以便数字可以放在相应的 Day 下?我可以使用 class 或 ruby 中的方法吗?顺便说一句,我不能使用 ruby 中的日期。
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
weekdays = "Su Mo Tu We Th Fr Sa" <<"\n"
month = "#{month_names} #{year}"
output =[
month.center(weekdays.size),
weekdays
].join("\n")
(1..length).each_slice(7) do |week|
output << week.join
output << "\n"
end
output
end
end
这是我从终端获得的结果:
Failure:
TestMonth#test_to_s_on_jan_2017 [test/test_month.rb:39]
Minitest::Assertion: --- expected
+++ actual
@@ -1,9 +1,8 @@
-" 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
-
+1234567
+891011121314
+15161718192021
+22232425262728
+293031
"
4 tests, 5 assertions, 3 failures, 0 errors, 0 skips
查看 String#rjust
和附带的示例。它会让您向字符串添加填充。
以下应将您想要的间距添加到一周中的每一天:
(1..length).each_slice(7) do |week|
output << week.map { |day| day.to_s.rjust(3) }.join
output << "\n"
end
但是查看您的预期输出,您想要删除每行的第一个 space。为此,试试这个:
(1..length).each_slice(7) do |week|
output << week.map { |day| day.to_s.rjust(3) }.join[1..-1]
output << "\n"
end
注意 [1..-1]
部分,这将删除每个 week
字符串的第一个字符。
旁注
附带说明一下,您不需要在方法中显式 return
或 class 变量,因为您已将它们定义为 attr_reader
.
此外,为了稍微清理一下,您甚至可以将 month_names
方法重构为类似于下面的代码(请注意,我已将方法名称更改为 month_name
)。 length
甚至 to_s
也可以这样做,但这可能是个人品味的问题。
def month_name
month_names[month - 1]
end
private
def month_names
%w(January February March April May June July August September October November December)
end