Ugly Code?

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
 
J

James Herdman

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 a little confused by your code. Some questions:

1. What is the parameter "d"? What is it's role in the overall picture?
2. a. What is the purpose of the while() loop? I can see how d_out
is being set, but are you expecting some sort of problem that would
require you to keep trying to find a new day?
b. Won't this while() loop only run once? d_out should only ever
be nil once, right?

James H.
 
D

Dan

Firstly, thanks for all the responses! I guess I should have been more
clear in my initial post. d is a Date object. The goal of the
function is to return a Date object for the same day in the next month.
For example, 2006-03-25 would become 2006-04-25. The while loop is
there to handle dates like 2006-01-31, which would need to become
2006-02-28 since there is no 31st in Feb.

Dan
 
C

ChrisH

Dan said:
Firstly, thanks for all the responses! I guess I should have been more
clear in my initial post. d is a Date object. The goal of the
function is to return a Date object for the same day in the next month.
For example, 2006-03-25 would become 2006-04-25. The while loop is
there to handle dates like 2006-01-31, which would need to become
2006-02-28 since there is no 31st in Feb.

Dan

If you look at the docs you'll see that next_month (or >>) already
adjust for months with differing end days.

One caveat, I guess next_month is a new method as it doesn't exist in
my install. >> works as described, and you can implement next_month,
or just gert the lates Date lib.

Cheers
 
J

James Herdman

If you look at the docs you'll see that next_month (or >>) already
adjust for months with differing end days.

One caveat, I guess next_month is a new method as it doesn't exist in
my install. >> works as described, and you can implement next_month,
or just gert the lates Date lib.

Cheers

I have Date#>> in my install -- Ruby 1.8.4. It definitely solves the
problem =)

James H.
 
D

Dan

Thanks Chris. I was being dense and automatically translating >> as +.
Being able to reduce my code to one line rocks!

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,070
Members
47,676
Latest member
scazeho

Latest Threads

Top