DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Emad has posted 58 posts at DZone. View Full User Profile

Calculate your age in years, days, hours, minutes, seconds in ruby

03.02.2014
| 2401 views |
  • submit to reddit
require 'time'

Date_of_birth = '1988-8-31'

# Credit to : http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails
# and modified a little bit
def humanize secs
  [	
  	[60, :seconds], 
  	[60, :minutes], 
  	[24, :hours], 
  	[365, :days], 
  	[100, :years]
  ].map do |count, name|
    if secs > 0
      secs, n = secs.divmod(count)
      "#{n.to_i} #{name}"
    end
  end.compact.reverse.join(' ')
end

loop do
	distance = Time.new - Time.parse(Date_of_birth)
	print humanize(distance)+"\r"
	sleep 1
end