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.