How to time something?

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

···

--
Posted via http://www.ruby-forum.com/.

Brian Geds wrote:

···

Heres what I have right now but it does reset the sec. to zero it keeps
going 61..62 how can I add more stuff reset it

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

--
Posted via http://www.ruby-forum.com/\.

Brian Geds wrote:

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?

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

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

(55..65).map {|secs| seconds_minutes_string(secs)} # => ["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)} # => ["0:55",
"0:56", "0:57", "0:58", "0:59", "1:00", "1:01", "1:02", "1:03",
"1:04", "1:05"]

···

On Tue, Nov 17, 2009 at 10:53 PM, Brian Geds <uforacer@hotmail.com> wrote:

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

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

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

First, as Michael pointed out, you probably want to divide modulo 60,
and you want the minutes to be based on seconds_elapsed rather than
seconds.

Second, each time you % and / the same number by the
same integer, you might want to use Numeric#divmod instead:

def time_str secs
  secs.divmod(60).join ':'
end

(57..62).map { |secs| time_str secs }
# => ["0:57", "0:58", "0:59", "1:0", "1:1", "1:2"]

Third, as Rick pointed out, if you prefer 1:02 to 1:2, you can do this:

def time_str secs
  '%d:%02d' % secs.divmod(60)
end

(57..62).map { |secs| time_str secs }
# => ["0:57", "0:58", "0:59", "1:00", "1:01", "1:02"]

— Shot (and, apparently, his ironic signature AI having a good day)

···

--
It’s great to be smart ’cause then you know stuff.