I want to create a subclass of Time, let’s say MyTime, so that I can define (especially formatting-related) methods without polluting the Time class.
class MyTime < Time; end
For some constructor methods defined on Time, I can use them in MyTime like this:
MyTime.now
but sometimes I need to create an instance of MyTime based on an instance of Time. For example, I would like to have an instance of MyTime based on:
File.mtime(path_to_some_file)
which is a Time instance. The constructor Time#new does not accept Time instances, so I cannot use that. How can I do it?
The
Timeclass has anatclass method that takes aTimeinstance as an argument:So you can say things like this:
So converting a
Timeto aMyTimeis a simple matter of callingMyTime.at.