I had to do some extensive operations with the Time class and even though it had a lot of features, it lacked the ones I used the most. It got so bad that at a point, I had to start using Ruby's method extension feature which helped to solve my problem to a great extent.
I would however, like to ask the Ruby developers to clean up their Time class (I am not the only one saying that, just do a google on Ruby's Time class and see what I mean).
To insert class methods is easy. Let me show you:
class << self
So now I can do this:
This allows me to insert custom Time creation methods where I can specify a time zone offset along with the time data I want.
What about some instance methods? How can I add for instance a date() method where I can tell Time.now to just give me the date component? Adding a class method won't help here. This requires a little hack (in my opinion).
First the Module:
# Converts the time to UTC and converts it
# into epoch in milliseconds
(to_f() * 1000.0).to_i()
# Gets only the Date component of the current
# time instance
Time.local(year, month, day, 0, 0, 0, 0)
And in the Time class:
class << include TimeInstance
Now I can say:
Now whatever you add in the TimeInstance module, will be added to the Time class's instance method list. The only catch is, you can only call public methods from the Time class in your module.
But this solves my problem greatly.
Hope this has helped you.