why doesn't this ruby code work -
here's practice question - write method take in number of minutes, , returns string formats number hours:minutes.
def time_conversion(minutes) hours = minutes / 60 mins = minutes % 60 time = hours + ":" + mins return time end the following tests see if works. if return true means code works correctly.
puts('time_conversion(15) == "0:15": ' + (time_conversion(15) == '0:15').to_s) puts('time_conversion(150) == "2:30": ' + (time_conversion(150) == '2:30').to_s) puts('time_conversion(360) == "6:00": ' + (time_conversion(360) == '6:00').to_s) sometimes true first 2 tests third test line shows false though code print out required.
other times following error:
string can't coerced fixnum (repl):4:in +' (repl):4:intime_conversion' (repl):1:in `initialize'
please assist.
the error refers line time = hours + ":" + mins
hours & mins fixnum, whereas ":" string error message indicates, "string can't coerced fixnum".
you either time = hours.to_s + ":" + minutes.to_s or time = "#{hours}:#{minutes}".
Comments
Post a Comment