Formatting Ruby's Prettyprint

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

Best way to pretty print a hash

require 'pp'
pp my_hash

Use pp if you need a built-in solution and just want reasonable line breaks.

Use awesome_print if you can install a gem. (Depending on your users, you may wish to use the index:false option to turn off displaying array indices.)

How to pretty-print decimal format

You could monkey-patch the method the pretty-printer uses. "Normal" IRb uses inspect, but most of the pretty-printer libraries have their own methods.

For example, the pp library from the standard library uses a method called pretty_print. BigDecimal doesn't have its own implementation of pretty_print unfortunately, it simply inherits the one from Numeric which just delegates to inspect.

So, we can write our own!

class BigDecimal
def pretty_print(pp)
pp.text to_s('F')
end
end

pp 10.to_d / 2.5
# 4.0

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"
}

Format Params in Rails Server Logs

If you want to render the parameters in a "pretty" way, you can convert them to hash. Although as you have unpermitted params, you should use to_unsafe_h(), which gives you an unsafe, unfiltered ActiveSupport::HashWithIndifferentAccess representation of the parameters. So:

pp params.to_unsafe_h

which will output something like:

{"id"=>"57dec892",
"resourceId"=>"a0d172yx",
"topic"=>"customer_bank_transfer_completed"}

Ruby pretty print isn't very pretty

For small Array (or Hash), the print is made on one single line, because it's (arguably) understandable at the first glimpse.

require 'pp'

ary = [1, 2, 3]
aaaaarrraaay = (1..100).to_a

pp ary
#> [1, 2, 3]
pp aaaaarrraaay
#> [1,
#> 2,
#> 3,
#> 4,
#> 5,
#> ...
#> 100]

This depends wether or not your printed output is above 79 chars. (see the code that does it)

However you can change that if your using PP.pp instead of pp

def my_pp(anything)
PP.pp(anything, out = $>, width = 0)
end

my_pp [1, 2, 3]
#> [1,
#> 2,
#> 3]

Pretty-print JSON document in Logstash 5

I think you're looking for the following:

filter {
ruby {
init => "require 'json'"
code => "event.set('message', JSON.pretty_generate(JSON.parse(event.get('message'))))"
}
}

Here is my test and the result:

input: {"make_me_pretty":"test"}
{
"@timestamp" => 2017-03-22T04:56:00.118Z,
"@version" => "1",
"message" => "{\n \"make_me_pretty\": \"test\"\n}"
}


Related Topics



Leave a reply



Submit