Created
November 6, 2012 12:55
-
-
Save borgand/4024548 to your computer and use it in GitHub Desktop.
Listen and Pry debugging testcase
This file contains 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
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