Here's a preview from my zine, The Pocket Guide to Debugging! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
get the zine!
read the transcript!
Sometimes you print out an object, and it just prints the class name and reference ID, like this:
MyObject<#18238120323>
Illustration of a frowning stick figure with curly hair.
person (thinking): “ugh, thanks, very helpful… "
Implementing a custom string representation for a class you’re often printing out can save a LOT of time. The name of the method you need to implement is:
- Python:
.__str__ - Ruby:
.to_s - JavaScript:
.toString - Java:
.toString - Go:
String()
Also, pretty-printing libraries (like pprint in Python or awesome_print in Ruby) are great for printing out arrays/hashmaps.