a RUBY error ... but still works
Posted: Wed Mar 27, 2019 4:03 pm
Hi Guys,
Wonder if someone could look at my Ruby code and identify why I keep seeing an error message in the Ruby status section.
I'm converting 'Seconds' into formatted HHMMSS. I see the result working ... but I get a 'NoMethodError: undefined method for'/' for "":String
thanks
Wonder if someone could look at my Ruby code and identify why I keep seeing an error message in the Ruby status section.
I'm converting 'Seconds' into formatted HHMMSS. I see the result working ... but I get a 'NoMethodError: undefined method for'/' for "":String
thanks
Code: Select all
# Will take as input a time in seconds (which is typically a result after subtracting two Time objects),
# and return the result in HH:MM:SS, even if it exceeds a 24 hour period.
def formatted_duration(total_seconds)
hours = @total_seconds / (60 * 60)
minutes = (@total_seconds / 60) % 60
seconds = @total_seconds % 60
[hours, minutes, seconds].map do |t|
# Right justify and pad with 0 until length is 2.
# So if the duration of any of the time components is 0, then it will display as 00
t.round.to_s.rjust(2,'0')
end.join(':')
end
output formatted_duration(@total_seconds)