Monkey Patching Time.strftime not being picked up by Time class

Go To


I need to Monkey patch strftime in Ruby 1.8.7 with Rails 2.3 on Windows. In config\initializers I put this time_patch.rb file (code below) but it does not seem to be picking up:

if RUBY_PLATFORM =~ /mingw32|mingw64|mswin32|mswin64/

  class Time
    alias_method :original_strftime, :strftime
    def strftime(fmt)
      hour12 = "%2d" % ((hour + 11) % 12 + 1)
      original_strftime(fmt.gsub(/%l/, hour12))


I renamed the method to def blorping and did Time.methods from the Rails console but did not see the new method.

What do I need to do to get it to work?

2012-04-04 19:51
by ScottJShea
what is your RUBY_PLATFORM - klochner 2012-04-04 20:09
@klochner It is i386-mingw3 - ScottJShea 2012-04-04 20:15
with 'def blorping . . .', try - klochner 2012-04-04 20:24
It is showing there... what did I do wrong; i.e. how do I get it added to the Time class - ScottJShea 2012-04-04 20:29
strftime is an instance method, that's what you wan - klochner 2012-04-04 20:35
@klochner Thank you for your help. As it was not in answer form I found a few other of your answers that were useful for me and voted them u - ScottJShea 2012-04-04 21:18
no problem, i don't need the karm - klochner 2012-04-04 22:57


You're checking the class methods when using Time.methods, so what you want is something like Time.instance_methods to be sure it's patched correctly.

2012-04-04 20:37
by tadman
Thank you! This exposed other issues I had with the environment/test - ScottJShea 2012-04-04 21:16
You can also check Time.instance_method(:strftime).source_location in Ruby 1.9 to see where the method is defined. The internal one should return nil, a sign it's probably implemented in C in the Ruby VM, but yours would show the source location - tadman 2012-04-04 21:42
I do not think source_location is available to me in the Ruby 1.8.7 I am tied to - ScottJShea 2012-04-04 22:03