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
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
Brian G. 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 itdef seconds_minutes_string(seconds_elapsed)
seconds = seconds_elapsed % 59
minutes = seconds / 60
return minutes.to_s + ‘:’ + seconds.to_s
end
Brian G. wrote:
Heres what I have right now but it does reset the sec. to zero it keeps
going 61…62def 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?
On Tue, Nov 17, 2009 at 10:53 PM, Brian G. [email protected]
wrote:
Heres what I have right now but it does reset the sec. to zero it keeps
going 61…62def seconds_minutes_string(seconds_elapsed)
seconds = seconds_elapsed % 59
minutes = seconds / 60
return 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 = 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”]
–
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 G.:
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 }
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 }
— Shot (and, apparently, his ironic signature AI having a good day)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs