Ruby/Rails Debugging: Break when a value of an object/variable changes

ruby, ruby-debug, ruby-on-rails

Solution

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

Problem

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 ?

Original source