awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.08k stars 454 forks source link

Prints attrs and instance vars #66

Closed phuibonhoa closed 12 years ago

phuibonhoa commented 12 years ago

Not sure if this is intended, but I just recently updated from an old version and discovered that ap now prints out attr_reader attributes, instance variables and the like. This results in a lot of extra output, that makes things so verbose it's hard to read. Just curious if anyone else is experiencing this and if there's a way to reduce the amount of amount from ap.

jeffdeville commented 12 years ago

Yeah, I've had to go back to pp myself for that reason.

michaeldv commented 12 years ago

Yes, displaying attributes and instance vars of an object is part of v1.0 release. What is the object that you're inspecting?

jeffdeville commented 12 years ago

For me, it's mostly mongoid objects

On Dec 13, 2011, at 7:16 PM, Michael Dvorkin reply@reply.github.com wrote:

Yes, displaying attributes and instance vars of an object is part of v1.0 release. What is the object that you're inspecting?


Reply to this email directly or view it on GitHub: https://github.com/michaeldv/awesome_print/issues/66#issuecomment-3133241

phuibonhoa commented 12 years ago

I have problems with ActiveRecord and ActiveRelation objects. Especially when you're dealing with an array of even two or three, it becomes really hard to read. Here's a super simple example where creating a new AR object prints with a ton of extra data:

Employee.create :username => 'john.smith', :name => 'John Smith', :email => 'John.Smith@email.com', :aim => 'jsmith', :gchat => 'jsmith@gmail.com', :twitter => 'jsmith'
#<Employee:0x1118ca340
    @attributes_cache = {
        "created_at" => Wed, 14 Dec 2011 03:00:37 UTC +00:00,
        "updated_at" => Wed, 14 Dec 2011 03:00:37 UTC +00:00
    },
    @destroyed = false,
    @marked_for_destruction = false,
    @new_record = false,
    @previously_changed = {
             "gchat" => [
            [0] nil,
            [1] "jsmith@gmail.com"
        ],
              "name" => [
            [0] nil,
            [1] "John Smith"
        ],
        "created_at" => [
            [0] nil,
            [1] Wed, 14 Dec 2011 03:00:37 UTC +00:00
        ],
        "updated_at" => [
            [0] nil,
            [1] Wed, 14 Dec 2011 03:00:37 UTC +00:00
        ],
          "username" => [
            [0] nil,
            [1] "john.smith"
        ],
               "aim" => [
            [0] nil,
            [1] "jsmith"
        ],
                "id" => [
            [0] nil,
            [1] 1
        ],
             "email" => [
            [0] nil,
            [1] "John.Smith@email.com"
        ],
           "twitter" => [
            [0] nil,
            [1] "jsmith"
        ]
    },
    @readonly = false,
    attr_accessor :attributes = {
             "gchat" => "jsmith@gmail.com",
              "name" => "John Smith",
        "created_at" => Wed Dec 14 03:00:37 UTC 2011,
            "github" => nil,
        "updated_at" => Wed Dec 14 03:00:37 UTC 2011,
          "username" => "john.smith",
                "id" => 1,
               "aim" => "jsmith",
            "mobile" => nil,
           "twitter" => "jsmith",
             "email" => "John.Smith@email.com"
    },
    attr_accessor :validation_context = nil,
    attr_reader :changed_attributes = {},
    attr_reader :errors = {}
gurgeous commented 12 years ago

I don't want to whine, but I find it a bit verbose as well. I would just like to reiterate that I love ap and I appreciate all your hard work keeping us whiners happy!

jeffdeville commented 12 years ago

Yeah, I have to agree with gurgeous. I apologize for being a whiner on this. Not cool, given the hard work, and value added to the community. My sincere apologies Michael.

michaeldv commented 12 years ago

My last commit takes care of extra noise for ActiveRecord models. By default only model attributes are shown unless you specify :raw => true. This change should be part of upcoming 1.0.2 release.

michaeldv commented 12 years ago

@jeffdeville Mongoid instances are going to be nicely formatted too. Check out https://github.com/michaeldv/awesome_print/commit/19c8e74e853f29f533ba88afa9065dab3d2f0cff commit by @gurgeous :-)

jeffdeville commented 12 years ago

Bad ass! Thanks!

On Dec 19, 2011, at 2:07 PM, Michael Dvorkin reply@reply.github.com wrote:

@jeffdeville Mongoid instances are going to be nicely formatted too. Check out https://github.com/michaeldv/awesome_print/commit/19c8e74e853f29f533ba88afa9065dab3d2f0cff commit by @gurgeous :-)


Reply to this email directly or view it on GitHub: https://github.com/michaeldv/awesome_print/issues/66#issuecomment-3207820

phuibonhoa commented 12 years ago

@michaeldv awesome news thank you! As everyone has said, we all love ap, so thank you for staying on top of it all and dealing with us whiners :)

michaeldv commented 12 years ago

Just pushed 1.0.2 to Rubygems

pangloss commented 12 years ago

Hey, this also causes an infinite loop/recursion if you have cyclical references in objects you're inspecting. I think this feature needs to be rethought or made optional. Has anyone else encountered this?

michaeldv commented 12 years ago

There's built-in mechanism to detect cyclical references based on object.object_id. For example:

 > data = [ false, 42, %w(forty two) ]
 > data << data  # <-- Nested array.
 > ap data, :multiline => false
 [ false, 42, [ "forty", "two" ], [...] ]

Do you have the code to reproduce the problem?

pangloss commented 12 years ago

Ok, I'm not 100% sure it's a cyclic thing, but if I inspect any object from the program I'm working on it works for a few minutes until the process runs out of memory. Unfortunately it's not an open source program that I can share. I'll see if I get the same behaviour with something open source or otherwise reproduce the problem. My simple attempts to reproduce it haven't worked so far.

pangloss commented 12 years ago

To give you an idea of how much stuff it's trying to output when I inspect an object, take a look at this gist:

The numbers it prints is how deeply nested the inspect is when it enters the awesome_hash method.

https://gist.github.com/1509116

Note that I only bothered monkey patching the awesome_hash method which is why the nesting levels are not sequential.

michaeldv commented 12 years ago

Could you please try this and see what happens:

require 'yaml'
y your_object

(basically try 'y' instead of 'ap').

pangloss commented 12 years ago

The data's not yaml compatible but maybe you'll get a vague sense of it from this: https://gist.github.com/83621e2e4b2c3b0aef4e though it's still pretty short on information.

Essentially there are a bunch of arrays/sets/hashes of objects that define the object, some of which reference the class that contains it and often other simillar classes as well, for instance to define links or dependencies between the classes.

michaeldv commented 12 years ago

What about introducing :depth option?

pangloss commented 12 years ago

That is possible, but really when you think about it, isn't it a bad idea to just automatically display all of a class's private instance variables? I would really hate as a library owner it if that extra transparency caused users to start to directly manipulate the private state of my objects and thereby make it impossible for me to refactor without breaking their code. The more I think about this feature the more convinced I am that it should be optional -- and default to off.

The problem with specifying depth is that it affects my ability to easily view other data structures, which is something I often want to do. In fact the ability to clearly see the contents of deeply nested hashes or arrays is the whole reason I use awesome print in the first place. To be forced to set a global depth defeats the purpose.

Thoughts?

Darrick

On Thu, Dec 22, 2011 at 1:51 AM, Michael Dvorkin < reply@reply.github.com

wrote:

What about introducing :depth option?


Reply to this email directly or view it on GitHub: https://github.com/michaeldv/awesome_print/issues/66#issuecomment-3246111

michaeldv commented 12 years ago

Well, 'y' from yaml standard Ruby library is also dumping all the instance variables and accessors. It has been this way for years and I haven't heard any requests to change that behavior. awesome_print just adds some better formatting but as far as the content goes it's all the same.

pangloss commented 12 years ago

That may be true, but 'y' isn't typically used in the console to display the output for user consumption so I don't think the comparison is really apt.

I'm also not saying that this isn't a very useful feature, in fact I can think of many situations that using ap to see instance variables will be very handy.

The only thing that I think is a mistake is to make it the default behaviour when I inspect a value in my console.

In addition, don't you think this behaviour is really broken in a situation like the following?

[34] pry(main)> class Password; def initialize; @pw = 'abc'; end ; def inspect; "(Password #{'' \ @pw.length })"; end end nil [35] pry(main)> Password.new

<Password:0x00000ff8

@pw = "abc"

A hallmark of an excellent Ruby library is that it displays useful and intelligent information in the inspect method. That is a long standing Ruby convention. Don't you think that this behaviour breaks some very valid assumptions about how Ruby objects are used?

Regards, Darrick

On Thu, Dec 22, 2011 at 2:26 AM, Michael Dvorkin < reply@reply.github.com

wrote:

Well, 'y' from yaml standard Ruby library is also dumping all the instance variables and accessors. It has been this way for years and I haven't heard any requests to change that behavior. awesome_print just adds some better formatting but as far as the content goes it's all the same.


Reply to this email directly or view it on GitHub: https://github.com/michaeldv/awesome_print/issues/66#issuecomment-3246305