D
Dan
Hi,
I'm new to ruby. I have a calendar on a web page and need to add
links to move to the next and previous months while keeping the
selected day within the month constant where possible. Here's what I
wrote to determine the next month, but it feels verbose and not very
ruby-like. Suggestions?
def get_next_month(d)
month = d.month
year = d.year
day = d.day
if month == 12
month = 1
year += 1
else
month +=1
end
d_out = nil
while !d_out
begin
d_out = Date.civil(year, month, day)
rescue
day -= 1
end
end
d_out
end
I'm new to ruby. I have a calendar on a web page and need to add
links to move to the next and previous months while keeping the
selected day within the month constant where possible. Here's what I
wrote to determine the next month, but it feels verbose and not very
ruby-like. Suggestions?
def get_next_month(d)
month = d.month
year = d.year
day = d.day
if month == 12
month = 1
year += 1
else
month +=1
end
d_out = nil
while !d_out
begin
d_out = Date.civil(year, month, day)
rescue
day -= 1
end
end
d_out
end