A simple age calculator for Ruby. Comes with age validator for ActiveModel.
You might think calculating age from a birthday is easy, but you'd be wrong - there is so much confusion on the topic, and sadly, many people are doing it wrong. Even right ways can look really complicated.
Here's the simplest, cleanest way:
(today.to_s(:number).to_i - birthday.to_s(:number).to_i) / 10000
Suppose today is May 16, 2014 and calculate the age of people whose birthdays are May 15, 1996 (should be 18 years old) and May 17, 1996 (should be 17 years old) respectively.
> 20140516 - 19960517
=> 179999
> 179999 / 10000
=> 17
> 20140516 - 19960515
=> 180001
> 180001 / 10000
=> 18
Couldn't be simpler. Note that Ruby truncates the decimal fraction.
Add this line to your application's Gemfile:
gem 'age_calculator'
Or install it yourself as:
$ gem install age_calculator
For a basic usage:
birthday = Date.new(1987,12,31)
AgeCalculator.new(birthday).age
=> 26
For a model with a validation on the age:
class Adult < ActiveRecord::Base
validates :birthday, age: { over: 18 }
end
- Fork it ( https://github.com/[my-github-username]/age_calculator/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request