How to time something?

B

Brian Geds

Heres what I have right now but it does reset the sec. to zero it keeps
going 61..62

def seconds_minutes_string(seconds_elapsed)
seconds = seconds_elapsed % 59
minutes = seconds / 60
return minutes.to_s + ':' + seconds.to_s
end
 
M

Michael W. Ryder

Brian said:
Heres what I have right now but it does reset the sec. to zero it keeps
going 61..62

def seconds_minutes_string(seconds_elapsed)
seconds = seconds_elapsed % 59
minutes = seconds / 60
return minutes.to_s + ':' + seconds.to_s
end

Shouldn't your modulus be 60 instead of 59 and shouldn't the minutes be
the seconds_elapsed divided by 60?
 
R

Rick DeNatale

Heres what I have right now but it does reset the sec. to zero =A0it keep= s
going 61..62

def seconds_minutes_string(seconds_elapsed)
=A0seconds =3D seconds_elapsed % 59
=A0minutes =3D seconds / 60
=A0return minutes.to_s + ':' + seconds.to_s
end

I'm not sure what you mean that "it doesn't reset the sec to zero

def seconds_minutes_string(seconds_elapsed)
seconds =3D seconds_elapsed % 59
minutes =3D seconds / 60
return minutes.to_s + ':' + seconds.to_s
end

(55..65).map {|secs| seconds_minutes_string(secs)} # =3D> ["0:55",
"0:56", "0:57", "0:58", "0:0", "0:1", "0:2", "0:3", "0:4", "0:5",
"0:6"]


This should produce the right strings, I assume you wanted 1:01
instead of 1:1 for 1 minute and 1 second

def seconds_minutes_string(seconds_elapsed)
"%d:%02d" % [seconds_elapsed / 60, seconds_elapsed % 60]
end

(55..65).map {|secs| seconds_minutes_string(secs)} # =3D> ["0:55",
"0:56", "0:57", "0:58", "0:59", "1:00", "1:01", "1:02", "1:03",
"1:04", "1:05"]





--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top