Skip to content

Instantly share code, notes, and snippets.

@borgand
Created November 6, 2012 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borgand/4024548 to your computer and use it in GitHub Desktop.
Save borgand/4024548 to your computer and use it in GitHub Desktop.
Listen and Pry debugging testcase
require 'listen'
require 'rb-readline' # <-- adding this fixes issues
require 'pry'
class MyGuard
attr_reader :thread
def initialize(dir)
callback = ->(*args){puts; puts "Got change: #{args.inspect}"}
@listener = Listen.to(dir).change(&callback)
end
def start
if !@thread || !@thread.alive?
@thread = Thread.new do
@listener.start
end
end
end
end
class MyInteractor
attr_reader :thread
def start
if !@thread || !@thread.alive?
@thread = Thread.new do
i = 0
while true
print "[#{i}] prompt> "
resp = gets.chomp
puts "DEBUG: received: #{resp}"
i += 1
end
end
end
end
end
class MyPry
attr_reader :thread
def start
if !@thread || !@thread.alive?
@thread = Thread.new do
Pry.start
exit
end
end
end
end
dir = Dir.pwd
puts "Listening to changes in '#{dir}'"
g = MyGuard.new dir
g.start
# Using MyInteractor works
#
# i = MyInteractor.new
# i.start
# i.thread.join
# Using MyPry blocks Listener
p = MyPry.new
p.start
p.thread.join
# This must be last thread, else the prompt won't be visible
g.thread.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment