auto-reload changed dependancies

K

Kevin Watt

Hi Rubyers

Does ruby keep a list of require'd files anywhere? I'd like to make a
method that will loop over them, check the last modification time to see
if they've, and reload them if they have.

One of the frustrating things about rails is that running it as a CGI to
have it auto-reload your changes can take 5-11 seconds to render a page
(at least on windows) - which makes testing a pain. I'm hoping to make
the ruby equilavant of Apache::StatINC in perl, but aren't sure of where
to begin, or if this has already been done before.

Thanks,
Kevin
Allpoetry.com Community Manager
 
M

Michael Neumann

Hi Rubyers

Does ruby keep a list of require'd files anywhere? I'd like to make a
method that will loop over them, check the last modification time to see
if they've, and reload them if they have.

I did that (or something very similar) yesterday for my webframework,
where a auto reload feature can be quite useful.

$LOADED_FEATURES and $LOAD_PATH is your friend.

# autoload.rb
START_TIME = Time.now
Thread.new {
file_mtime = {}
loop do
sleep 1
$LOADED_FEATURES.each do |feature|
$LOAD_PATH.each do |lp|
file = File.join(lp, feature)
if File.exists?(file) and File.stat(file).mtime > (file_mtime[file] || $START_TIME)
file_mtime[file] = File.stat(file).mtime
p "reload #{ file }"
load(file)
end
end
end
end
}

Regards,

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top