awesome-print/awesome_print

Is there a reason why we are not Awesome-Printing Class instance variables?

habermeier opened this issue · 1 comments

I think it'd be nice if a Class could get awesome printed just like an object. Is there a reason why not?

class Foo 
    attr_accessor :bar
end

a = Foo.new
a.bar = 'hello'

and...

> ap a
#<Foo:0x007ff107d76810 @bar="hello">

I could convert the class attributes into a hash and ap that, but that seems kind of cumbersome:

> a.instance_variables.each_with_object({}) { |v,h| h[v.to_s[1..-1]] = a.instance_variable_get(v) }
{
    "bar" => "hello"
}

While I saw some other tickets around wanting to distinguish between a class, certainly that could be taken care of in the first line, by perhaps pre-pending the class of the object?

Am I just confused?

You can already do this by providing ap with raw: true:

> ap a, raw: true
#<Foo:0x02e6e298
    attr_accessor :bar = "hello"
>

As for the reason why this behavior probably will never be the default: real objects tend to be large (with many recursive references and stuff). Consider printing an instance of ActionDispatch::Journey::Route from Rails with the raw option set to true — it causes the terminal to hang for several seconds, and outputs thousands of lines as the result, which cannot be reasonable inspected anyway.