Note: This is using rails core extensions. It'd need to be modified a bit for a pure ruby implementation.
def underage?(birthday) 18.years.ago < birthday end
11384 users tagging and storing useful source code snippets
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
def underage?(birthday) 18.years.ago < birthday end
class Integer def underage?( birthday ) self.years.ago < birthday end end
21.underage?( birthday )
ArgumentError: comparison of Time with Date failed from (irb):3:in `<' from (irb):3:in `underage?' from (irb):6
1.year #=> 31557600 (which is 365.25 days times the number of seconds in a day)
validates_each :birthdate do |record, attr, value| record.errors.add.attr, "You're not old enough." if value > Date.new((Date.today.year - 18),(Date.today.month),(Date.today.day)) end
def age age = Date.today.year - read_attribute(:birthdate).year if Date.today.month < read_attribute(:birthdate).month || (Date.today.month == read_attribute(:birthdate).month && read_attribute(:birthdate).day >= Date.today.day) age = age - 1 end return age end
You need to create an account or log in to post comments to this site.
so you could also use this to see if somebody is under 21 by calling