In Ruby on Rails while debugging is there any way where we can ask the debugger to break the execution as soon as a value at specific memory location or the value of a variable/object changes ?
1 Answer
How much of a break in execution do you want?
If the variable is set from outside the instance, then it will be being accessed via some method. You could overwrite such a method just for this purpose.
# define
class Foo
def bar
@bar ||= 'default'
end
def bar=(value)
@bar = value
end
end
# overwrite
class Foo
def bar=(value)
super
abort("Message goes here")
end
end
2 Comments
Kinaan Khan Sherwani
In my case I don't know where the value is changing. i.e. i don't know about the exact function/location of the code where the change happening. So I wanted to use such functionality of debugger ( if available ) that whenever that value changes the debugger breaks execution and i will be able to locate that particular piece of code.
S.Spencer
This is just an indication of a strategy for finding where the change is being called from. If it is internal to the instance, you could ensure all access to the variable goes though the getter/setter methods. If it is from an external call, then a similar line of reasoning applies. If, however, the data is being changed with a bulk update of the db table, then all bets are off.