Get person's age in Ruby
I know I'm late to the party here, but the accepted answer will break horribly when trying to work out the age of someone born on the 29th February on a leap year. This is because the call to birthday.to_date.change(:year => now.year)
creates an invalid date.
I used the following code instead:
require 'date'
def age(dob)
now = Time.now.utc.to_date
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
end
How to display ages instead of dates of birth, in Ruby on Rails?
Rails has a couple of Date-Helpers, among them distance_of_time_in_words: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words.
As far as I remember it adds an "about x years", so it might be not useful in your particular situation unless you take a look at the source and adapt it.
Otherwise you can calculate with years directly calling Date.today.year (or dob.year).
Age calculation in Ruby
For the validator, you can use a custom method:
validate :over_18
def over_18
if dob + 18.years >= Date.today
errors.add(:dob, "can't be under 18")
end
end
Ruby age method using date of birth including months
While this may be better posted to a codereview site, I will still give you my thoughts.
You have written a fairly long method for what could be a couple smaller ones.
First, I would write one method that takes the number of years an months and separate that into its own function.
def readable_age(years, months)
# for under 1 year olds
if years == 0
return months > 1 ? months.to_s + " months old" : months.to_s + " month old"
# for 1 year olds
elsif years == 1
return months > 1 ? years.to_s + " year and " + months.to_s + " months old" : years.to_s + " year and " + months.to_s + " month old"
# for older than 1
else
return months > 1 ? years.to_s + " years and " + months.to_s + " months old" : years.to_s + " years and " + months.to_s + " month old"
end
end
Though, if you do not mind adding some dependencies to your project, you can take advantage of the actionview
gem, you can take advantage of the pluralize
function. Something along the lines of
def readable_age(years, months)
year_text = ''
if years != 0
year_text = "#{years} #{pluralize('year', years)} and "
end
"#{year_text}#{pluralize('month', months)} old"
end
Now for your function to calculate the number of years and months.
def age(t)
dob = self.date_of_birth
months = (t.year * 12 + t.month) - (dob.year * 12 + dob.month)
# months / 12 will give the number of years
# months % 12 will give the number of months
readable_age(months / 12, months % 12)
end
EDIT
The reason I am passing a date object into the age
function is to allow you to calculate the the age of a person for a given time stamp. It also makes it easier to test a function if it produces the same result given the same inputs.
Find the name and age of the oldest person in a txt file using ruby
"figure out who the oldest person/people are on this list", so multiple results are possible. Ruby has a group_by method, which groups an enumerable by a common property. What property? The property you specify in the block.
grouped = File.open('nameage.txt') do |f|
f.group_by do |line|
line.split(":").last.to_i # using OP's line
end
end
p grouped # just to see what it looks like
puts grouped.max.last # end result
Related Topics
Find Out If Current Time Is Between Two Times
To_D to Always Return 2 Decimals Places in Ruby
How to Get the Current Absolute Url in Ruby on Rails
How to Reload the Current Page in Ruby on Rails
Validation for Non-Negative Integers and Decimal Values
How to Convert a Bigdecimal to a 2-Decimal-Place String
Case Statement With Multiple Values in Each 'When' Block
Cannot Load Such File - Zlib Even After Using Rvm Pkg Install Zlib
Is Ruby Pass by Reference or by Value
Why Do Ruby Setters Need "Self." Qualification Within the Class
How to Dynamically Create a Local Variable
Understanding the Rails Authenticity Token
How to Understand Nil Vs. Empty Vs. Blank in Ruby
Difference Between "And" and && in Ruby