[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ugly Code?

Dan

4/12/2006 4:59:00 PM

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

8 Answers

rhild

4/12/2006 6:01:00 PM

0


Dan,

How about this:

def next_month(d)
d >> 1
end

James Herdman

4/12/2006 6:06:00 PM

0

> 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.

Chris Hulan

4/12/2006 6:08:00 PM

0

Wouldn't this suffice?:

def get_next_month(d)
return d>>1
end


Cheers

Chris Hulan

4/12/2006 6:19:00 PM

0

Actually, after RTFMing a bit, I see that Date#next_month(n=1) does
just that (see
http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.ht...)

cheers

Dan

4/12/2006 6:25:00 PM

0

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

Chris Hulan

4/12/2006 6:37:00 PM

0


Dan wrote:
> 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

James Herdman

4/12/2006 6:52:00 PM

0

On 2006-04-12 14:37:01 -0400, "ChrisH" <chris.hulan@gmail.com> said:

>
> Dan wrote:
>> 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

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

James H.

Dan

4/12/2006 6:54:00 PM

0

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

Dan