Created
October 30, 2008 05:21
-
-
Save postmodern/20920 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# | |
# Name: reval.rb | |
# License: MIT | |
# Author: postmodern (postmodern.mod3 at gmail.com) | |
# Description: | |
# | |
# Re-evaluates a specified Ruby file whenever the file changes. | |
# Reval was inspired by Giles Bowkett's kickass talk on Archaeopteryx at | |
# RubyFringe 2008, where Giles used some mad Ruby to re-evaluate his | |
# Achaeopteryx script as he edited it. | |
# | |
# Reval might come in handy, when you give that awesome breakthrough talk | |
# at some conference. | |
# | |
require 'digest/md5' | |
# | |
# Re-evaluate the contents of the specified _file_, whenever the file | |
# changes, using the given _options_. | |
# | |
# _options_ may contain the following keys: | |
# <tt>:pause</tt>:: Number of seconds to sleep between checking the _file_ | |
# for changes. Defaults to +0.4+ if not given. | |
# | |
# reval 'file.rb' | |
# | |
# reval 'file.rb', :pause => 0.4 | |
# | |
def reval(file,options={}) | |
pause = (options[:pause] || 0.4) | |
last_time = Time.now | |
this_time = Time.now | |
last_fingerprint = nil | |
fingerprint = nil | |
if File.file?(file) | |
last_fingerprint = Digest::MD5.hexdigest(File.read(file)) | |
load(file) | |
end | |
loop do | |
begin | |
this_time = File.mtime(file) | |
if (this_time > last_time) | |
fingerprint = Digest::MD5.hexdigest(File.read(file)) | |
if last_fingerprint != fingerprint | |
load(file) | |
last_fingerprint = fingerprint | |
end | |
last_time = this_time | |
end | |
rescue Errno::ENOENT | |
end | |
sleep(pause) | |
end | |
end | |
reval(ARGV[0]) if ($0 =~ /reval/ && ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment