Uh oh!
There was an error while loading. Please reload this page.
Add hook for customizing the debug representation of an object - #1005
Add hook for customizing the debug representation of an object#1005amomchilov wants to merge 8 commits into
Conversation
| @debug_representation = debug_representation | ||
| end | ||
| def debug_representation |
There was a problem hiding this comment.
Perhaps it should take unused rest args, in case any need to be added in the future?
| defdebug_representation | |
| defdebug_representation(*, **) |
jabamaus
commented
Sep 21, 2023
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
commented
Sep 25, 2023
Could you give us the API specification? |
jabamaus
commented
Sep 26, 2023
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
commented
Sep 26, 2023
@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 Look closely at the PR description, I put an |
jabamaus
commented
Sep 27, 2023
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
commented
Sep 27, 2023
@jabamaus Please have a look at the implementation. It always uses the |
ko1
commented
Oct 2, 2023
Could you explain the specification of |
These values are assumed to be 0 if they're missing.
8942797 to
8a6053bCompare✅ All Tests passed!✖️no tests failed ✔️669 tests passed(1 flake) |
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
OpenStructandActionController::StrongParameters.example.rbI chose the spelling
#debug_representation, because it's clear and to-the-point, but it's open to ideas.