Skip to content

Instantly share code, notes, and snippets.

View jvon1904's full-sized avatar

Jesse vB jvon1904

View GitHub Profile
@jvon1904
jvon1904 / copy_json.rb
Last active October 28, 2024 14:04
Ruby - pretty format and copy valid JSON or Ruby Hash to clipboard or pasteboard.
# NB: This method utilizes 'pbcopy' which is only available on MacOS.
#
# I often find myself in the IRB or Pry console needing to both format a Ruby Hash and copy it to the clipboard.
# Here is a covenient method that can accomplish both of those tasks
#
# Consider this example:
#
# A co-worker asks you to text them the data from the most recent Post in your Rails app.
#
# You enter the rails console...
@jvon1904
jvon1904 / json_pretty_inline.rb
Last active September 11, 2024 18:13
Ruby, Pretty generate inline JSON
# Below is a way to add a JSON module method #pretty_inline to receive a Ruby Hash and generate pretty JSON.
# This is similar to #pretty_generate only instead of adding indentation and newlines, this
# will produce an inline String.
#
# Under the hood, the method uses the JSON#generate method which accepts a 2nd optional argument of options.
#
# :object_nl (object newline) - normally a newline character (\n) but in this case, just a space. This will be
# inside curly braces and after commas
#
# :array_nl (array newline) - normally a newline character (\n) but in this case, just a space as well. This will