Scrutinizing Your Code
When things aren’t working the way you expect them to, you obviously need to find out why. There are certain tricks that can make this task a lot easier on you, and you can use them without ever needing to fire up the debugger.
Utilizing Reflection
Many bugs come from using an object in a different way than you’re supposed to, or from some internal state deviating from your expectations. To be able to detect and fix these bugs, you need to be able to get a clear picture of what is going on under the hood in the objects you’re working with.
I’ll assume that you already know that Kernel#p and Object#inspect exist, and how to use them for
basic needs. However, when left to their default behaviors, using these
tools to debug complex objects can be too painful to be practical. We
can take an unadorned Prawn::Document’s inspect
output for an example:
#<Prawn::Document:0x12cf17c @page_content=#<Prawn::Reference:0x12cecf4 @data={:Length=>0}, @gen=0, @identifier=4, @stream="0.000 0.000 0.000 r g\n0.000 0.000 0.000 RG\nq\n", @compressed=false>, @info= #<Prawn::Reference:0x12cf0c8 @data={:Creator=>"Prawn", :Producer=>"Prawn"}, @gen=0, @identifier=1, @compressed=false> , @root=#<Prawn::Reference:0x12cf064 @data={:Type=>:Catalog, :Pages=> #<Prawn::Reference:0x12cf08c @data={:Count=>1, :Kids=>[#<Prawn::Reference:0x12ceca4 @data={:Contents=>#<Prawn::Reference:0x12cecf4 @data={:Length=>0}, @gen=0, @identifier=4, @stream="0.000 0.000 0.000 rg\n0.000 0.000 0.000 RG\nq\n", << ABOUT ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access