Andrew Bezzub
Andrew Bezzub

Reputation: 16032

Ruby: find where global variable is changed

Some gem in my tests sets $stdout to dev/null. Is there a way to raise an error whenever $stdout is changed? Or is there some other way to track what changes it?

Update: For anyone who might have similar issue - the problem was that quietly and silence_stream methods of ActiveSupport are not thread safe. In this case activerecord-session_store is the offending gem, see https://github.com/rails/activerecord-session_store/pull/22

Upvotes: 4

Views: 78

Answers (1)

Amadan
Amadan

Reputation: 198324

set_trace_func proc { |event, file, line, id, binding, classname|
  if $stdout != STDOUT
    STDOUT.puts "$stdout changed at #{file}:#{line}"
    exit
  end
}

Hmm. I guess that would actually be the line after the change, technically... If you care, you can remember the previous file/line in each trace step, and print that instead.

Upvotes: 5

Related Questions