Skip to content

Instantly share code, notes, and snippets.

@nikz
Created December 4, 2008 23:58
Show Gist options
  • Save nikz/32157 to your computer and use it in GitHub Desktop.
Save nikz/32157 to your computer and use it in GitHub Desktop.
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