Convert Dog's Age to Human Years using Ruby

Use you may use the following simple Ruby script to convert a dog's age to human years.

Convert Dog's Age to Human Years using Ruby
Photo by Karsten Winegeart / Unsplash

Use you may use the following simple Ruby script to convert a dog's age to human years.

dog_age = 6 # replace with the actual age of your dog
human_age = 0

if dog_age == 1
  human_age = 15
elsif dog_age == 2
  human_age = 24
else
  human_age = 24 + (dog_age - 2) * 4.5
end

puts "A #{dog_age}-year-old dog is #{human_age} years old in human years."

Notes

  • If the dog's age is 1, the script sets the human age to 15.
  • If the dog's age is 2, the script sets the human age to 24.
  • If the dog's age is greater than 2, the script uses the formula human_age = 24 + (dog_age - 2) * 5 to calculate the dog's age in human years.