Ruby 我怎样才能使一个月的日期按每天打印(例如:Su Mo Tu We…等)?
我有一个数字为1-31的大字符串。我怎样才能把这个月的名字放在中间呢 我的代码:Ruby 我怎样才能使一个月的日期按每天打印(例如:Su Mo Tu We…等)?,ruby,calendar,Ruby,Calendar,我有一个数字为1-31的大字符串。我怎样才能把这个月的名字放在中间呢 我的代码: 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
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
Ruby中的String类有一个方法:
以下内容并不能真正回答您的问题。这只是对您的代码的完全重写,因为我很无聊:
require 'date'
class Month
attr_reader :month, :year
def initialize(month, year)
@month = month
@year = year
end
def first_of_month
Date.new(year, month, 1)
end
def last_of_month
Date.new(year, month, -1)
end
def month_name
last_of_month.strftime('%B')
end
def days_in_month
last_of_month.day
end
def to_s
[].tap do |out|
out << header
out << weekdays
grouped_days.each do |days|
out << days.map { |day| day.to_s.rjust(2) }.join(' ')
end
end.join("\n")
end
private
def header
"#{month_name} #{year}".center(weekdays.size)
end
def weekdays
'Su Mo Tu We Th Fr Sa'
end
def grouped_days
days = (1..days_in_month).to_a
first_of_month.wday.times { days.unshift(nil) }
days.each_slice(7)
end
end
Month.new(4, 2015).to_s
# April 2015
# 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
需要“日期”
班级月份
属性读取器:月:年
def初始化(月、年)
@月=月
@年=年
结束
def每月的第一天
新日期(年、月、1)
结束
def最后一个月
新日期(年、月、-1)
结束
def月名
月的最后一个月。strftime(“%B”)
结束
定义月内天数
每月最后一天
结束
def至美国
[]点击“完成”|
出去,非常感谢你。你知道我怎样才能把一个月的每一天都打印出来吗?我真的很感谢你的帮助!我看到你加了看跌期权。但是我怎么能用它来代替puts呢?我尽量不使用date。他们的方式不同吗?put
不需要,我把它删除了。我想这将很难避免日期。您必须自己执行闰年(简单)和工作日()的规则。我不确定它是否值得,因为Date
是一个Ruby核心类,因此总是可用的。
weekdays = "Su Mo Tu We Th Fr Sa"
month = "#{month_names} #{year}"
output = [
month.center(weekdays.size),
weekdays
].join("\n")
puts output
# April 2015
# Su Mo Tu We Th Fr Sa
require 'date'
class Month
attr_reader :month, :year
def initialize(month, year)
@month = month
@year = year
end
def first_of_month
Date.new(year, month, 1)
end
def last_of_month
Date.new(year, month, -1)
end
def month_name
last_of_month.strftime('%B')
end
def days_in_month
last_of_month.day
end
def to_s
[].tap do |out|
out << header
out << weekdays
grouped_days.each do |days|
out << days.map { |day| day.to_s.rjust(2) }.join(' ')
end
end.join("\n")
end
private
def header
"#{month_name} #{year}".center(weekdays.size)
end
def weekdays
'Su Mo Tu We Th Fr Sa'
end
def grouped_days
days = (1..days_in_month).to_a
first_of_month.wday.times { days.unshift(nil) }
days.each_slice(7)
end
end
Month.new(4, 2015).to_s
# April 2015
# 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