Skip to content

Instantly share code, notes, and snippets.

@livmaria7891
Last active August 10, 2016 15:52
Show Gist options
  • Save livmaria7891/9021707d593952e6578fc394be713d23 to your computer and use it in GitHub Desktop.
Save livmaria7891/9021707d593952e6578fc394be713d23 to your computer and use it in GitHub Desktop.
Hours in a Year homework
#Note: the 'age_in_seconds' method works fine as a method in irb but does not consistently work in this code?
print "What would you like to know?\nA: How many days, hours, and minutes are in a number of years\nB: How many minutes are in a decade\nC: How many seconds old you are\nD: Convert your age in seconds to years\n"
choice = gets.chomp.upcase
def time_in_years
print "Years:"
years = gets.chomp.to_i
days = 365 * years
hours = 24 * days
minutes = 60 * hours
seconds = 60 * minutes
puts "There are #{days} days in #{years} year(s).\nThere are #{hours} hours in #{years} year(s).\nThere are #{minutes} minutes in #{years} year(s).\nThere are #{seconds} seconds in #{years} year(s)."
end
def minutes_in_decade
min = 10 * (365*8) +(366*2) * 24 * 60
puts "There are #{min} minutes in a decade!"
end
def age_in_seconds
print "How old are you?"
years = gets.chomp.to_f
in_seconds = years * 365 * 24 * 60 * 60
print "On your last birthday, you were #{in_seconds} seconds old."
end
def seconds_to_years
print "How many seconds old are you?"
age_seconds = gets.chomp.to_f
seconds_per_year = 60*60*24*365
age_years = (age_seconds/seconds_per_year).to_i
puts "You are " + age_years.to_s + " years old."
end
case choice
when "A"
time_in_years
when "B"
minutes_in_decade
when "C"
age_in_seconds
when "D"
seconds_to_years
else
puts "You didn't pick A, B, C, or D."
end
@CheezItMan
Copy link

Good use of a case and methods. Also what about Ada's age?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment