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

Left pad Node#inspect hex digits with zeros. #54

Merged
merged 3 commits into from
Mar 8, 2022

Conversation

havenwood
Copy link
Contributor

This tries to adjust Node #inspect to more closely resemble the vanilla format by adding left padded zeros. I don't know if less padding would be preferred, but it seems nice to left pad a bit so printed hierarchies align and such. Maybe eight hex digits?

For comparison, this is before.

#<Async::Node:0x8c>
	#<Async::Node:0x3c>
		#<Async::Node:0x50>
			#<Async::Node:0x64>
	#<Async::Node:0x78>

And after.

#<Async::Node:0x0000000000000118>
	#<Async::Node:0x0000000000000078>
		#<Async::Node:0x00000000000000a0>
			#<Async::Node:0x00000000000000c8>
	#<Async::Node:0x00000000000000f0>

Or with 8 rather than 16 for padding.

#<Async::Node:0x00000118>
	#<Async::Node:0x00000078>
		#<Async::Node:0x000000a0>
			#<Async::Node:0x000000c8>
	#<Async::Node:0x000000f0>

Or even just 6.

#<Async::Node:0x000118>
	#<Async::Node:0x000078>
		#<Async::Node:0x0000a0>
			#<Async::Node:0x0000c8>
	#<Async::Node:0x0000f0>

I also added a left shift of the object ID, like has been done with inspect in recent Rubies, but I'm unsure if that's still worth doing now that Ruby 2.7 is doing IDs differently.

Ruby 2.6:

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

format('%#018x', object_id << 1)
#=> "0x0000000114858260"

Ruby 2.7

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

format('%#018x', object_id << 1)
"0x0000000000000190"

I don't know if you want to left pad or left shift but I got to making the PR before I realized object IDs changed in Ruby 2.7. Are sixteen hex digits too many to pad? I personally prefer padding 8 to avoid the varied length.

Thanks for the great library in any case!

@ioquatix
Copy link
Member

ioquatix commented Jan 9, 2020

Please follow width of Ruby 2.7 and add a spec for the formatting. Thanks.

@coveralls
Copy link

coveralls commented Jan 9, 2020

Coverage Status

Coverage increased (+3.9%) to 89.599% when pulling d1da060 on havenwood:object_id into ce8bc07 on socketry:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+3.8%) to 89.507% when pulling aa7b754 on havenwood:object_id into ce8bc07 on socketry:master.

@havenwood havenwood changed the title Left shift object IDs and pad them with zeros. Left pad object IDs with zeros. Jan 9, 2020
@havenwood havenwood changed the title Left pad object IDs with zeros. Left pad Node#inspect hex digits with zeros. Jan 9, 2020
@havenwood
Copy link
Contributor Author

Updated, added tests and rebased.

@olleolleolle
Copy link
Contributor

@ioquatix This looks ready to go in!

@ioquatix
Copy link
Member

ioquatix commented Mar 8, 2022

Thanks. I'm going to merge this. I might try it out before releasing it or I might modify it, but having some extra specs is great. The main thing is that object ids and memory addresses are different. I feel like formatting rules might need to reflect that, since typically object addresses are formatted with enough digits because we know the minimum and maximum bounds. For Ruby object ids, we could even know that and it won't be 2^64 for sure.

@ioquatix ioquatix merged commit 3cd035f into socketry:main Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants