I frequently encounter erroneous nil values when running tests. In order to find the problem, I end up inserting a bunch of lines like:
puts 'variable x is nil' if x.nil?
puts 'variable y is nil' if y.nil?
...
Which slowly gets me closer to the problem. Is there a gem or something that can quickly print a list of all nil variables in the current scope? Or some method of debugging my methods that’s a little less laborious?
If you’re using so-called
printf-debugging to track down these problems you’re probably doing it wrong. You probably want to be writing tests that assert the presence of a value, then assert additional conditions:It seems odd you’re getting such a large number of
nilvalues that you need to go out of your way to track them down. Generally they’re produced at a particular point in time, so trap them as soon as possible to avoid having to check them later: