Advertisement
saasbook

humanize_time_answer.rb

Jan 10th, 2012
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.66 KB | None | 0 0
  1. require 'time'
  2. class Time
  3.   private
  4.     def hour_to_words(hr)
  5.       puts self
  6.       case hr
  7.       when 12 then "noon"
  8.       when 0,24 then "midnight"
  9.       else ['zero','one','two','three','four','five','six','seven',
  10.           'eight','nine','ten','eleven'][hr % 12]
  11.       end
  12.     end
  13.   public
  14.   def humanize
  15.     "About " +
  16.       case self.min # what minute is it?
  17.       when 0..7   then "#{hour_to_words(hour)}"
  18.       when 8..22  then "a quarter after #{hour_to_words(hour)}"
  19.       when 23..37 then "half past #{hour_to_words(hour)}"
  20.       when 38..52 then "a quarter til #{hour_to_words(hour+1)}"
  21.       when 53..59 then "#{hour_to_words(hour+1)}"
  22.       end
  23.   end
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement