I have a number of seconds. Let's say 270921. How can I display that number saying it is xx days, yy hours, zz minutes, ww seconds?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- How to account for clock offsets in a distributed
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
Rails has an helper which converts distance of time in words. You can look its implementation: distance_of_time_in_words
I modified the answer given by @Mike to add dynamic formatting based on the size of the result
I just start writing ruby. i guess this is only for 1.9.3
It can be done pretty concisely using
divmod
:You could probably DRY it further by getting creative with
collect
, or maybeinject
, but when the core logic is three lines it may be overkill.Needed a break. Golfed this up:
You can use the simplest method I found for this problem:
You can always adjust returned value to your needs.