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

options classname and object_id #296

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ raw: false, # Do not recursively format instance variables.
sort_keys: false, # Do not sort hash keys.
limit: false, # Limit arrays & hashes. Accepts bool or int.
ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
classname: :class, # Methods used to get Instance class name.
object_id: true, # Show object id.
color: {
args: :pale,
array: :white,
Expand Down
4 changes: 3 additions & 1 deletion lib/awesome_print/formatters/object_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def valid_instance_var?(variable_name)
end

def awesome_instance
"#{object.class}:0x%08x" % (object.__id__ * 2)
str = object.send(options[:classname]).to_s
str << ":0x%08x" % (object.__id__ * 2) if options[:object_id]
str
end

def left_aligned
Expand Down
2 changes: 2 additions & 0 deletions lib/awesome_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(options = {})
sort_keys: false, # Do not sort hash keys.
limit: false, # Limit arrays & hashes. Accepts bool or int.
ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
classname: :class, # Method used to get Instance class name.
object_id: true, # Show object_id.
color: {
args: :pale,
array: :white,
Expand Down
50 changes: 50 additions & 0 deletions spec/objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,55 @@ def initialize
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
#<Hello
@abra = 1,
@ca = 2,
@dabra = 3
>
EOS
expect(out).to be_similar_to(str)
expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
end

it 'classname' 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, classname: :to_s)
str = <<-EOS.strip
#<CustomizedHello:placeholder_id
@abra = 1,
@ca = 2,
@dabra = 3
>
EOS
expect(out).to be_similar_to(str)
expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
end


end
end