DEV Community

Cassidy
Cassidy

Posted on • Originally published at cassidy.codes on

5

Favourite Debugging Trick in Ruby

Last week I wrote about some of my favourite debugging tools in Ruby. I forgot one trick that I find super useful!

Today I was writing a script that would iterate through objects in an S3 bucket and group these objects by similar names. The naming structure looked like this:

original.jpg
tumb_original.jpg
profile_original.jpg
Enter fullscreen mode Exit fullscreen mode

So all three of those images should get grouped together because they all come from orignal.jpg. But I got my name comparison muddled and something wasn’t working right. It would work perfectly for 200 images then it blew up on the 201st!

Printing logs or putting a binding in a loop like this is gnarly. I don’t want to loop through 200 successful iterations to find what I need!

Conditional Bindings!

To solve this problem, I just made my binding conditional.

s3_objects.each do |img|
  binding.pry if img.key =~ /badfilename.jpg/
end
Enter fullscreen mode Exit fullscreen mode

This time we’ll only hit the binding if the image key contains the filename that’s giving me grief!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay