public
Created

Listen and Pry debugging testcase

  • Download Gist
testlistener.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.