Created
December 4, 2008 23:58
-
-
Save nikz/32157 to your computer and use it in GitHub Desktop.
This file contains 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
class Time | |
cattr_accessor :frozen_to | |
class << self | |
def now_with_freezing | |
if @@frozen_to | |
@@frozen_to | |
else | |
now_without_freezing | |
end | |
end | |
alias_method_chain :now, :freezing | |
end | |
end | |
def freeze_time(time = Time.now) | |
Time.frozen_to = time | |
yield | |
ensure | |
Time.frozen_to = nil | |
end | |
# == watch the magic == | |
freeze_time do | |
puts Time.now | |
sleep 5 | |
puts Time.now | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment