Pretty Print to a File in Ruby

Formatting Ruby's prettyprint

#!/usr/bin/ruby1.8

require 'pp'
mooth = [
"booth", "month", "mooch", "morth",
"mouth", "mowth", "sooth", "tooth"
]
PP.pp(mooth, $>, 40)
# => ["booth",
# => "month",
# => "mooch",
# => "morth",
# => "mouth",
# => "mowth",
# => "sooth",
# => "tooth"]
PP.pp(mooth, $>, 79)
# => ["booth", "month", "mooch", "morth", "mouth", "mowth", "sooth", "tooth"]

To change the default with a monkey patch:

#!/usr/bin/ruby1.8

require 'pp'

class PP
class << self
alias_method :old_pp, :pp
def pp(obj, out = $>, width = 40)
old_pp(obj, out, width)
end
end
end

mooth = ["booth", "month", "mooch", "morth", "mouth", "mowth", "sooth", "tooth"]
pp(mooth)
# => ["booth",
# => "month",
# => "mooch",
# => "morth",
# => "mouth",
# => "mowth",
# => "sooth",
# => "tooth"]

These methods also work in MRI 1.9.3

How to pretty format JSON output in Ruby on Rails

Use the pretty_generate() function, built into later versions of JSON. For example:

require 'json'
my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_object)

Which gets you:

{
"array": [
1,
2,
3,
{
"sample": "hash"
}
],
"foo": "bar"
}

Ruby pretty print a BSON document?

While I haven't used mongodb or BSON before, if we approach this from a JSON perspective you have a couple options. First, instead of calling doc.to_json, it looks like that uses a method as_json to get a hash representation, that it then turns into a string, so we'll use doc.as_json as a starting point instead.

doc = {"_id":{"$oid":"5a64d2ce36ab1f1ea4b06228"},"admin":"1234","users":"12345","house":{"a":0,"b":0,"c":0},"room":{"a":0,"b":2,"c":1}}

The first way is to use the PP library which is a pretty printer for ruby:

require 'pp'
pp doc
# Outputs:
# {:_id=>{:$oid=>"5a64d2ce36ab1f1ea4b06228"},
# :admin=>"1234",
# :users=>"12345",
# :house=>{:a=>0, :b=>0, :c=>0},
# :room=>{:a=>0, :b=>2, :c=>1}}

which is close to what you want, the pp library has a way to define a customized output by defining a #pretty_print(pp) function in your class, that you can use to help get it the rest of the way.

The second option to look into is JSON.pretty_generate

require 'json'
puts JSON.pretty_generate(doc)
# {
# "_id": {
# "$oid": "5a64d2ce36ab1f1ea4b06228"
# },
# "admin": "1234",
# "users": "12345",
# "house": {
# "a": 0,
# "b": 0,
# "c": 0
# },
# "room": {
# "a": 0,
# "b": 2,
# "c": 1
# }
# }

Which is a bit closer to what you're wanting. With some post-processing you can get rid of those brackets and commas, if you want:

puts JSON.pretty_generate(doc).delete('{},').gsub(/\n\s*\n/, "\n")

As for why switching from attributes to keys starts outputting only the keys, well attributes looks and sounds like it would be a hash, thus looping through you have 2 elements being passed to the block each time, and keys sounds like an array, so you only get the name of the attribute. As mentioned, I don't know mongo, but you would then need to somehow look up the attribute with the key named on your model to get it's value. For instance in ActiveRecord, you might use a public_send(key) if you weren't able to use its attributes method.

Pretty print a JSON object created in Ruby to Chrome browser console

Expanding on Cba Bhusal's answer, this worked for me the best:

console.log("<%= j JSON.pretty_generate(@search.attributes).html_safe %>");

The escape_javascript was needed to escape quotes, while the JSON.pretty_generate inserted whitespace to make it even prettier. The output in Chrome console looks like this:

{
"id": 138,
"user_id": null,
"created_at": "2015-01-04T15:57:23.110Z",
"updated_at": "2015-01-04T15:57:23.110Z"
#...
}


Related Topics



Leave a reply



Submit