How do I format the output showing Rails objects

I’ve had this question for a long time, but I’ve not asked it before. When looking at a problem, I sometimes get a massive lump of text, which is a dump of a Ruby/Rails object. It has no new lines and includes nested objects, arrays and much more. Here is a tiny example (separated into lines at each comma):

[#<Rails::Initializable::Initializer:0x00007fcd8ca8c260
  @name="register caststone photos plugin",
  @context=#<Refinery::Caststone::Engine:0x00007fcd8c5dfe88
  @_all_autoload_paths=[
     "..../refinerycms-caststone/lib",

I can start trying some indenting, but with over 2000 lines its hard to sort out what I’ve got.

My questions are

  • What is this format called?
  • What file extension should I give it so that my IDE/editor can format it for me.
  • Is there an IDE which knows about it?

thanks for any help, Anita

1 Like

An exact match for what you’re looking for might not exist, but for working in a console, Pretty Print might get you close.

With your own objects you can override the inspect method to output something custom.

2 Likes

Agree with Eric. Try pretty print or “pp” in the console. You can also look into using awesome printwhich tends to provide additional formatting and structure to inspect these.

For especially long or nested object operations it can be helpful to use the pluck method to inspect a single attribute or filter method to select a segment of them. Combining these can give you quicker insight into the objects you’re dealing with.

Thanks @Eric_Hayes and @mzagaja, I have awesome_print, and use it when I can.

It’s when I am not in a console, or using a debugger that the problem arises.

In the current case, I have a stacklevel too deep problem, and I am chasing it in initializers and the route mapper.

Perhaps, I should write a pp for this text dump format myself, but that is not going to move me any further ahead with my current problem (not soon, anyway).

Anita

One thing I have relied on in the past is this online formatter: https://codebeautify.org/ruby-formatter-beautifier which not only can do a Ruby object notation (what I think you’re looking at in your case) but also JSON and XML. When I have a one-off lump of text that I need to unspool and make more visually understandable, I will often use that or one of its variants.

Walter

1 Like

That looks so much better. Thank you very much

There is an open PR to improve the inspect output of common Rails classes and fix the unworkably large exception objects.