I found one source which successfully overrode Time.strftime
like this:
class Time
alias :old_strftime :strftime
def strftime
#do something
old_strftime
end
end
The trouble is, strftime
is an instance method. I need to override Time.now
- a class method - in such away that any caller gets my new method, while the new method still calls the original .now
method. I've looked at alias_method
and have met with no success.
This is kinda hard to get your head around sometimes, but you need to open the "eigenclass" which is the singleton associated with a specific class object. the syntax for this is class << self do...end.
class Time
alias :old_strftime :strftime
def strftime
puts "got here"
old_strftime
end
end
class Time
class << self
alias :old_now :now
def now
puts "got here too"
old_now
end
end
end
t = Time.now
puts t.strftime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With