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
Rails: Url/Path with Parameters
Dynamic CSS in Rails Asset Pipeline, Compile on Fly
Solving the Travelling Salesman Problem in Ruby (50+ Locations)
Xpath Parent Attribute of Selection
Raise Exception When Accessing Attributes That Doesn't Exist in Openstruct
Decoding Facebook's Signed Request in Ruby/Sinatra
Has Anyone Figured Out a Way to Run the Same Cucumber Scenario on Multiple Browsers/Web Drivers
Why Do I Get "Including Capybara::Dsl in the Global Scope Is Not Recommended!"
How to Stop God from Leaving Stale Resque Worker Processes
Is There a Ruby Http Client Library with a Response Cache
Best Way to Work with Large Amounts of CSV Data Quickly
Rspec Testing Has_Many :Through and After_Save
Error Installing Gem: Couldn't Reserve Space for Cygwin's Heap, Win32 Error 487
Differencebetween 'Size' and 'Length' Methods