Skip to content

Add hook for customizing the debug representation of an object - #1005

Draft
amomchilov wants to merge 8 commits into
ruby:masterfrom
Shopify:debug-representation-hook
Draft

Add hook for customizing the debug representation of an object#1005
amomchilov wants to merge 8 commits into
ruby:masterfrom
Shopify:debug-representation-hook

Conversation

@amomchilov

@amomchilovamomchilov commented Jul 28, 2023

Copy link
Copy Markdown

This PR depends on the changes in #1004. Only this last commit of this PR is unique to this change.

Description

I propose we add a hook method that developers can implement in their classes to customize the structural representation of their objects in a debugger. Used judiciously, this has the potential to really improve the clarify of certain types of objects.

I found it particularly useful for container-like objects, like OpenStruct and ActionController::StrongParameters.

BeforeAfter
Screenshot 2023-07-28 at 10 12 48 AMScreenshot 2023-07-28 at 10 17 11 AM
example.rb
require'ostruct'require'action_controller'classOpenStruct# Use our hash representation directly, to hide the `@table` clutter.defdebug_representation=to_hendclassActionController::Parameters# Flatten the parameters right into the top level of this object's representation.defdebug_representation{:@permitted=>@permitted,:@logging_context=>@logging_context,
**@parameters}endendline=OpenStruct.new(start_point: OpenStruct.new(x: 1,y: 2),end_point: OpenStruct.new(x: 3,y: 4),)params=ActionController::Parameters.new({person: {name: "Francesco",age: 22,role: "admin"}})

I chose the spelling #debug_representation, because it's clear and to-the-point, but it's open to ideas.

@debug_representation = debug_representation
end

def debug_representation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it should take unused rest args, in case any need to be added in the future?

Suggested change
defdebug_representation
defdebug_representation(*, **)

@amomchilovamomchilov changed the title Debug representation hookAdd hook for customizing the debug representation of an objectJul 28, 2023
@jabamaus

Copy link
Copy Markdown

A big +1 for this feature. This would be great. Surprised this wasn't in from the start. Being able to control what's on the debug inspector without overloading to_s and inspect (which may be needed for something else) seems like a very sensible idea.

When are some of these PRs going to get applied? It's been quiet the last few months...

@ko1

ko1 commented Sep 25, 2023

Copy link
Copy Markdown
Collaborator

Could you give us the API specification?

@jabamaus

Copy link
Copy Markdown

I'm not sure what the content of this PR exactly is but what I'd simply like is the ability to define a method on my objects called something like "to_rdbg" (or something I don't really mind) and have rdbg call that preferentially to to_s/inspect. In this way I could have to_s/inspect freed up for just my use and control over what appears in the debug window.

@amomchilov

Copy link
Copy Markdown
Author

@jabamaus@ko1 Ignore the first few commits that come from other branches. This is the relevant commit to this PR: 8942797

@ko1 The API is really simple: If your object responds to #debug_representation, the debugger will call it and use the result as the representation of your object.

Look closely at the PR description, I put an example.rb that shows an examples for OpenStruct and ActionController::Parameters.

@jabamaus

Copy link
Copy Markdown

Thanks @amomchilov that makes it easier to follow. What will happen if the object being inspected is a BasicObect? BasicObject does not implement respond_to?

@amomchilov

Copy link
Copy Markdown
Author

@jabamaus Please have a look at the implementation. It always uses the Kernel implementation of respond_to?, so that it works even on BasicObject, or any objects with a bad override of #respond_to?.

@ko1

ko1 commented Oct 2, 2023

Copy link
Copy Markdown
Collaborator

@ko1 The API is really simple: If your object responds to #debug_representation, the debugger will call it and use the result as the representation of your object.

Could you explain the specification of #debug_representation?

@amomchilov
amomchilovforce-pushed the debug-representation-hook branch from 8942797 to 8a6053bCompareNovember 14, 2024 21:31
@cloudbees-smart-tests

cloudbees-smart-testsBot commented Nov 14, 2024

Copy link
Copy Markdown

All Tests passed!

✖️no tests failed ✔️669 tests passed(1 flake)

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@amomchilov@jabamaus@ko1@ono-max