Rails Byebug gem: How to create a conditional breakpoint on a variable assignment?

434 views Asked by At

How do I create a conditional breakpoint in Byebug about a variable assignment? This is more an event than a condition. But how would you proceed if you do not know where in the code the assignment could happen?

Watching a variable in step by step manner isn't delivering results. I would need to watch it, until some arbitrary value is assigned to the identifier.

I only know that an Identifier is somewhere in the Rails program, maybe even in the initialization process, is assigned a value to. I don't know when or what. The "what" is what I want to find out. Is there a debugging solution?

1

There are 1 answers

0
Mori On

E.g. if you know that the variable starts off as nil

byebug unless x.nil?

Then scatter that statement around your code wherever that variable is in scope and you suspect it may be changed. Or if you want to restrict it to a certain operation or loop, set a global variable where you want to start looking like

$enable_byebug = true if name == 'foo'
do(something)
...
byebug if !x.nil? && $enable_byebug

This is one of the few contexts where I'm happy to use global variables.