Forum: Ruby Re: convert seconds to hours:minutes:seconds

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2005-12-14 12:11
(Received via mailing list)
Everytime you use printf, God kills a kitten.

time = 7683
p [time/3600, time/60 % 60, time % 60].map{|t| t.to_s.rjust(2,
'0')}.join(':')

=> "02:08:03"

cheers

Simon
C6858f4b8ec263c8c3c58759494e926a?d=identicon&s=25 Andy Delcambre (Guest)
on 2005-12-14 23:20
(Received via mailing list)
What is wrong with printf, I learned C first and most recently have
done a lot of programming with python which includes printf style
variable interpolation. It is my favorite way of formatting output as
it is what I know.  Does god kill a kitten out of personal preference
or some deep seated problem with printf in ruby?

 - Andy Delcambre
Fee23d1fc58edee59e05d7a52dcf172e?d=identicon&s=25 Kevin Brown (Guest)
on 2005-12-15 00:24
(Received via mailing list)
On Wednesday 14 December 2005 16:19, Andy Delcambre wrote:
> What is wrong with printf, I learned C first and most recently have
> done a lot of programming with python which includes printf style
> variable interpolation. It is my favorite way of formatting output as
> it is what I know.  Does god kill a kitten out of personal preference
> or some deep seated problem with printf in ruby?

C/C++ programmers are comfortable with printf.  If someone overloaded
the %
operator and started using that in C++, you'd get annoyed.  It's not
something that's a widely used portion of the language.  So if anyone
else
ever has to read your code, you're asking them to approach it from the C
point of view instead of the Ruby point of view.  Being that the common
denominator is that they'll know Ruby, this is inconsiderate.

Can you do it?  Sure.  But Ruby programmers, as a rule, do not think
about
string processing this way.  At least save yourself some keystrokes and
use
the exact same format string and % if you must.

>  - Andy Delcambre

[*snip*]
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2005-12-15 01:48
(Received via mailing list)
On Thu, 15 Dec 2005, Kevin Brown wrote:

> ever has to read your code, you're asking them to approach it from the C
> point of view instead of the Ruby point of view.  Being that the common
> denominator is that they'll know Ruby, this is inconsiderate.
>
> Can you do it?  Sure.  But Ruby programmers, as a rule, do not think about
> string processing this way.  At least save yourself some keystrokes and use
> the exact same format string and % if you must.

sure.  but here you have

   harp:~ > irb
   irb(main):001:0> Time::now.strftime '%H:%M:%S'
   => "17:44:28"

in case op didn't know...

regards.

-a
This topic is locked and can not be replied to.