This is a job interview problem. I'm supposed to create a data structure for a time in seconds and milliseconds, then create two Time objects, and then write a function that can return the difference between the two Times. This is my code:
class Time
def initialize (sec, milli_sec)
@sec = sec
@milli_sec = milli_sec
end
def difference(time_2)
puts @sec.to_i*1000 + @milli_sec.to_i + time_2.@sec
end
end
time_1 = Time.new('5','30')
time_2 = Time.new('6','40')
time_1.difference(time_2)
This is the error:
syntax error, unexpected tIVAR, expecting '('
I am having a problem accessing the @sec, @milli_sec variables of time_2 passed as time_1.difference(time_2). I think that the syntax is [email protected]_i or time_2.#@sec.to_i, but those return errors. time_2.sec returns uninitialized time, even though it looks like it's been initialized. I would like to know the solution to this problem.
intvalues duringinitialize?