33
loading...
This website collects cookies to deliver better user experience
byebug
or binding.pry
. During this past month, I've been learning techniques that let me level up these skills.byebug
based debugging.p
instead of puts
. puts
calls to_s
on the object, which by default is the object class and id
. You can override the to_s
class to return detailed information about the object. The other option is to use p
, which calls .inspect
on the object. By default, inspect returns a string with the class, object_id, and instance variables. The output of p
can be difficult to parse if an object has many instance variables. In this case, you can use pp
, which stands for pretty print, and makes the output easier to read. pp
is also helps to format hashes and JSON objects.freeze
it. Then whenever the object is modified, it will raise an exception. Freezing an object is a faster way to figure out which classes are modifying it.inspect
and pp
are useful but don't always appear in beginner ruby tutorials. I've found that learning more about ruby has given me new tools for debugging Ruby code. There are many methods in ruby that are there to make it easier for developers to work with ruby.input_obj
) but it's not clear what type of input it is. Normally, I would search the code base for all the locations that this method is invoked. In the calling method, you can figure out what is passed in as the input. A faster way to figure this out would be to run the code and do p input_obj.class.name
. That way, you know the exact class of the input. Everything in ruby is an object and inherits from the Object
class class. It has methods such as methods
, instance_variables
, responds_to?
that you can use to learn more about method inputs. Granted, you can figure out a lot of this information with inspect
.caller
method. You can use caller
to get the calling stack for an object. caller
is a faster way to figure out who is calling a method instead of searching through the entire code base.source_location
on the method:ClassName.instance_method(:method_name).source_location
source_location
is especially useful when the method name is common and is harder to search for in the code. If a method calls super
, you can use super_method
to get the Method
object for the super method:ClassName.instance_method(:method_name).super_method
included
. You can overwrite included
to print information when a module is included on an object. Use method_added
to track when an instance method is added to a module. These methods help track down bugs related to metaprogramming.trace = TracePoint.new(:call) do |tp|
p[tp.path, tp.lineno, tp.defined_class, tp.method_id]
end
trace.enable
User.some_method
trace.disable
tp.path
), the line number tp.lineno
, the class tp.defined_class
, and the method tp.method_id
.TracePoint.trace(:call) do |tp|
next unless tp.self.is_a?(User) # only print method calls for Users
# tracing logic
end
bundle open <gem_name>
to open the code for the gem in a text editor. It will open the version specified in the nearest Gemfile. That way, you can use your IDE to search and navigate the gem code. In your application code, you can use source_location
to find the location of a method defined in a gem! You can also use print statements and byebug
to debug the gem source code if needed. When you finish debugging, use gem pristine <gem_name>
to clean up any changes.