awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

Pretty print object as executable code #426

Open forthrin opened 1 year ago

forthrin commented 1 year ago

If there a way to make this (or another) gem print objects on a form that:

  1. generates the same object (ie. can be preceded by foo = and run as code)
  2. passes Rubocop validation
  3. has smart options for things like
    • contracting short arrays on a single line
    • replacing (recent) timestamps with Time.now
    • replace JSON with {...}.to_json
    • replace query URLs with something like "https://domain/path/?#{URI.encode_www_form({a: 1, b: 2, ...})"
one = [
  false, 42, %w[forty two],
  { now: Time.now, class: Time, distance: 4.2e+43 }
]
ap(one)
[
  false, 42, %w[forty two],
  { now: Time.now, class: Time, distance: 4.2e+43 }
]

For me personally it doesn't need support for classes, methods, etc. as I assume that's the 80% of the Pareto principle.