From 36e486dbc13c6d4ec2ffa79da6ca56d7fcd63b78 Mon Sep 17 00:00:00 2001 From: Aaron Heesakkers Date: Tue, 10 Jan 2017 14:48:36 +0100 Subject: [PATCH] options classname and object_id # Conflicts: # spec/objects_spec.rb --- README.md | 2 + .../formatters/object_formatter.rb | 4 +- lib/awesome_print/inspector.rb | 2 + spec/objects_spec.rb | 49 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 716c207..7160932 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ sort_keys: false, # Do not sort hash keys. sort_vars: true, # Sort instance variables. limit: false, # Limit arrays & hashes. Accepts bool or int. ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. +class_name: :class, # Method called to report the instance class name. (e.g. :to_s) +object_id: true, # Show object id. color: { args: :pale, array: :white, diff --git a/lib/awesome_print/formatters/object_formatter.rb b/lib/awesome_print/formatters/object_formatter.rb index a63d0f5..49334f7 100644 --- a/lib/awesome_print/formatters/object_formatter.rb +++ b/lib/awesome_print/formatters/object_formatter.rb @@ -60,7 +60,9 @@ module AwesomePrint end def awesome_instance - "#{object.class}:0x%08x" % (object.__id__ * 2) + str = object.send(options[:class_name]).to_s + str << ":0x%08x" % (object.__id__ * 2) if options[:object_id] + str end def left_aligned diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 9686a08..3c3fe11 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -23,6 +23,8 @@ module AwesomePrint sort_vars: true, # Sort instance variables. limit: false, # Limit arrays & hashes. Accepts bool or int. ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. + class_name: :class, # Method used to get Instance class name. + object_id: true, # Show object_id. color: { args: :pale, array: :white, diff --git a/spec/objects_spec.rb b/spec/objects_spec.rb index f4bdde4..3053ee0 100644 --- a/spec/objects_spec.rb +++ b/spec/objects_spec.rb @@ -167,5 +167,54 @@ EOS expect(out).to be_similar_to(str) expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect) end + + it 'object_id' do + class Hello + def initialize + @abra = 1 + @ca = 2 + @dabra = 3 + end + end + + hello = Hello.new + out = hello.ai(plain: true, raw: true, object_id: false) + str = <<-EOS.strip +# +EOS + expect(out).to be_similar_to(str) + expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect) + end + + it 'class_name' do + class Hello + def initialize + @abra = 1 + @ca = 2 + @dabra = 3 + end + + def to_s + "CustomizedHello" + end + end + + hello = Hello.new + out = hello.ai(plain: true, raw: true, class_name: :to_s) + str = <<-EOS.strip +# +EOS + expect(out).to be_similar_to(str) + expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect) + end + end end