Domanda

I have the following code:

puts 12.months.ago.month

Currently, this displays 5, for May since it's May 1st.

I need to spoof the date to make Rails think that it is yesterday, April 30th, so that the above will display 4. I am running this on my local machine - I tried changing my computers date/time, but it didn't work as intended.

Any thoughts?

È stato utile?

Soluzione

class Time
  class << self
    alias :orig_now :now
    def now
      orig_now - 1.day
    end
  end
end

Time.now
# => 2014-04-30 16:35:01 +0300 

puts 12.months.ago.month
# => 4

Altri suggerimenti

Why don't you do something like following:

puts (12.months.ago - 1.day).month         # returns 4
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top