From: Daniel Waite Date: 2007-12-03T06:25:41+09:00 Subject: Re: Name this method... thefed wrote: > On Dec 2, 2007, at 3:58 PM, Daniel Waite wrote: > >>>> 75018.to_elapsed_time >> => {:days=>0, :minutes=>50, :seconds=>18, :hours=>20} >>>> 789010.to_elapsed_time >> => {:days=>9, :minutes=>10, :seconds=>10, :hours=>3} > > That's DEFINITELY a nice method to have around :-) Thank you! > Unfortunately.... > >> started = Time.now >> ended = Time.now + some_time_into_the_future >> elapsed = (ended.to_i - started.to_i).to_elapsed_time > > puts ended - started > #=> Some Time > > already works I aware of that, but didn't want a float due to its inaccuracies. (Though I admit I'm still uncertain exactly HOW those inconsistencies manifest themselves, I know they can be wrong from time to time.) I suppose I could write.. (ended - started).to_i > Don't trash your method though! It will probably come in handy one day Thank you again! :) I've added elapsed to Time, which piggybacks off Fixnum's implementation (that sounds bad... is it?) class Fixnum def seconds_into_time elapsed_time = Hash.new elapsed_time[:days], remainder = self.divmod(86400) elapsed_time[:hours], remainder = remainder.divmod(3600) elapsed_time[:minutes], remainder = remainder.divmod(60) elapsed_time[:seconds] = remainder elapsed_time end end class Time def elapsed(time) (time - self).to_i.seconds_into_time end end As you can see I renamed it to #seconds_into_time. Any thoughts on that name? I like the to_xxx family of methods, but... 100.to_time_breakdown 100.from_seconds_into_time_breakdown 100.to_time 100.to_elapsed_time None of them feel right. I'm sad. =( -- Posted via http://www.ruby-forum.com/.