Skip to content

Instantly share code, notes, and snippets.

@floere
Created June 29, 2012 01:36
Show Gist options
  • Save floere/3015156 to your computer and use it in GitHub Desktop.
Save floere/3015156 to your computer and use it in GitHub Desktop.
Live Reloading of Code: Presentation at Melbourne Roro
# Version 1: Fails, since this file is not reloaded.
#
module Loader
def self.reload
puts "Loading application."
load File.expand_path "../worker.rb", __FILE__ # Change this to new_worker
load File.expand_path "../signal.rb", __FILE__
end
end
# # Version 2: Succeeds, since it also reloads the file itself.
# #
# module Loader
#
# def self.reload
# load_self
# load_app
# end
#
# def self.load_app
# puts "Loading application."
# load File.expand_path "../worker.rb", __FILE__ # Change this to new_worker
# load File.expand_path "../signal.rb", __FILE__
# end
#
# def self.load_self
# puts 'Loader loading itself.'
# load __FILE__
# end
#
# end
class Worker
def work
sleep 0.3 # Melbourne is faster.
puts "It's totally well known that Melbourne has the best coffee!"
end
end
Florian Hanke - @hanke
PhD Student Melbourne Uni
CTO Technology Astronauts
1. What does require do?
file.load unless file.already_required?
(Using a hash, thanks to Xavier Shay)
2. Demo: Reloading code in a running server
require File.expand_path '../loader', __FILE__
# Load everything explicitly.
#
Loader.reload
# Simulates e.g. a server taking requests.
#
worker = Worker.new
loop do
worker.work
end
Signal.trap 'USR1' do
Loader.reload
end
class Worker
def work
sleep 1 # Sydney: Slow!
puts "Sydney has the best coffee in the world!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment