Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class_name option doesn't seem to do anything #72

Open
ghost opened this issue Apr 17, 2021 · 1 comment
Open

class_name option doesn't seem to do anything #72

ghost opened this issue Apr 17, 2021 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@ghost
Copy link

ghost commented Apr 17, 2021

In both Rails 6 console and IRB, (and in the original AwesomePrint gem as well), the class_name option doesn't seem to have any effect.

The class name seems to always be printed as the result of calling to_s on the object, no matter what the class_name option is set to.

o = Object.new
#<Object:0x00007fed5affae48>

ap o
#<Object:0x00007fed5affae48>

ap o, class_name: :to_yaml
#<Object:0x00007fed5affae48>

The reason I'm trying to use this is, I have an ActiveRecord model with a to_s instance method, and want to print out the class/model name when I print it to console or debug log. Example:

class Member < ApplicationRecord
...
    def to_s
        full_name
    end
...
end

ap Member.first

Nathan Fahey {
               :id => "096e9d23-2c66-4e11-8110-50e60e59c05f",
         :zip_code => "10014",
       :first_name => "Nathan",
        :last_name => "Fahey",
...
}

I would like to be able to see the class name, like when I do:

ap Member
class Member < ApplicationRecord {
               :id => :uuid,
         :zip_code => :string,
       :first_name => :string,
        :last_name => :string,
  ...
}

But when I do:

ap Member.first, class_name: :class

or set class_name to any string or symbol, I get the result of Member.first.to_s for the class name.

Also, setting the option, object_id: false does not seem to help.

@HarlemSquirrel
Copy link
Member

Hi @tgrushka and thanks for taking the time to open this issue.

I've not used this feature myself or even tried it since I forked the repo but I spent some time poking around today and it seems the :class_name option only works with the ObjectFormatter today which is only used when raw: true is set. Additionally, the class name option is only used when object_id: false option is set.

Here's an examaple of it working today:

require_relative 'lib/amazing_print'

class Person
  attr_accessor :name
  attr_reader :created_at

  def initialize
    @created_at = Time.now
  end
end

class Wizard < Person
  attr_accessor :sword
end

person = Wizard.new
person.name = 'Gandalf'
person.sword = 'Glamdring'

puts "\n Just ap"
ap person

puts "\n Now ap with class_name: :name"
ap person, class_name: :name, object_id: false, raw: true

Here's the output I get

image

I'm definitely open to improving this so :class_name is used in more places.
It does make sense to me to omit the object ID when using custom class names to avoid confusion about the real class name so that part may just need clarifying in our documentation.

@HarlemSquirrel HarlemSquirrel added the documentation Improvements or additions to documentation label Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant