Created
September 10, 2012 21:14
-
-
Save HotFusionMan/3693902 to your computer and use it in GitHub Desktop.
A way to edit the methods of the actual Time class for testing purposes. Useful in situations where a fake class (e.g., https://gist.github.com/3672210 ) can't be substituted, such as when third-party code that uses Time is involved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Redefine Time.now to be one day earlier than the real thing. | |
| # N.B. Using the "alias" keyword does not work correctly -- in the following, it would cause infinite stack recursion. Cf. http://www.ruby-forum.com/topic/135598 | |
| class Time | |
| class << self | |
| alias_method :original_now, :now | |
| def now | |
| original_now - 86400 | |
| end | |
| end | |
| end | |
| # try calling Time.now now to see the effect | |
| # Put back the original definition. | |
| class Time | |
| class << self | |
| alias_method :now, :original_now | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment