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 out attributes hash, even on objects with overridden #to_s and #inspect #127

Open suan opened 11 years ago

suan commented 11 years ago

In Rails, if I have a model with an overridden to_s and inspect, and I use AwesomePrint.irb in console, it correctly prints my overridden string, but then it appends the object's attributes to the end of it. This is pretty annoying - Is this expected behavior?

clonezone commented 11 years ago

I consider this to be a huge feature. I consider most #to_s and #inspect implementations broken from a debugging standpoint and wish to ignore them all.

suan commented 11 years ago

I guess we have to agree to disagree, especially for to_ ses that you've implemented yourself. Is there at least a way to disable it though? Any way would do...

Sent from my iPhone

On Jul 10, 2013, at 11:55 AM, Elliot Shank notifications@github.com wrote:

I consider this to be a huge feature. I consider most #to_s and #inspect implementations broken from a debugging standpoint and wish to ignore them all.

— Reply to this email directly or view it on GitHub.

hagabaka commented 11 years ago

Sometimes instance variables are too complex to print, and it's definitely useful for debugging to be able to skip these. pp respects custom inspect methods, and alternatively lets you define pretty_print_instance_variables on a class to only print those instance variables. Could you add the ability to hide specific instance variables at least?

imajes commented 9 years ago

I was bitten by this again yesterday, but i’m not convinced of the best way to deal with it. Ideas/suggestions welcome. :)

bruno- commented 9 years ago

Hi, I also experienced this with an object that inherits from Struct. The object had to_s, inspect and pretty_print methods defined but it was still displayed as a Struct.

I know too little about how the gem works but I'd be happy if it respected inspect or at least pretty_print methods defined on a class itself.

bruno- commented 9 years ago

After playing with this locally I managed to make awesome_print respect the inspect method defined on a class inheriting from Struct.

Here's the example class and a "fix":

class SomeClass < Struct.new(:var)
  def inspect
    "SomeClass #{some_method}"
  end

  def some_method
    var.to_s + foo
  end
end
# lib/awesome_print/formatter.rb
def awesome_struct(s)
  # inspect method must be defined on an object itself
  if s.public_methods(false).include?(:inspect)
    s.inspect
  else
    hash = {}
    s.each_pair { |key, value| hash[key] = value }
    awesome_hash(hash)
  end
end

I know too little about the project to think this is a valid solution. But if you think this might be okay, I'd be happy to work on proper implementation (tests, making sure it works on older ruby versions).

duffyjp commented 8 years ago

I think we should be able to define our own inspect and have awesome print use that. If that's not considered reasonable, perhaps AP could check if an object responds to awesome_inspect or something that we could return whatever we want.

githorse commented 7 years ago

+1. Bummed that there's no way to customize display of my Structs.