Skip to content

Instantly share code, notes, and snippets.

@HotFusionMan
Created September 10, 2012 21:14
Show Gist options
  • Select an option

  • Save HotFusionMan/3693902 to your computer and use it in GitHub Desktop.

Select an option

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.
# 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