Created
May 24, 2016 16:53
-
-
Save carlzulauf/280dfe3601672e3daa1facaf2eaf6664 to your computer and use it in GitHub Desktop.
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
# A simple tripwire utility to trigger breakpoints selectively. | |
# | |
# Requires `pry` and provides a pry console at the breakpoints. | |
# | |
# Place `binding.trip` anywhere you might want a breakpoint in your code | |
# | |
# def some_method(args) | |
# do_something | |
# binding.trip # <= breakpoint here | |
# do_something_else | |
# end | |
# | |
# When you want that binding.trip to act like binding.pry, wrap it like so | |
# | |
# Tripwire.set { some_method(:foo) } | |
# | |
# You can also turn it on, do stuff, then turn it off elsewhere. | |
# | |
# Tripwire.set | |
# some_method(:foo) | |
# Tripwire.unset | |
# | |
class Tripwire | |
class << self | |
def on? | |
@@on ||= false | |
end | |
def set | |
@@on = true | |
if block_given? | |
yield | |
@@on = false | |
end | |
end | |
def unset | |
@@on = false | |
end | |
end | |
end | |
class Object | |
def trip(*a) | |
pry(*a) if Tripwire.on? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment