Created
November 15, 2025 19:38
-
-
Save havenwood/152081cf69f5f9e2de7dc93fbf0093d6 to your computer and use it in GitHub Desktop.
A nicer interface to look at the Shape of a Ruby Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'json' | |
| require 'objspace' | |
| class RubyVM | |
| module Shape | |
| def self.id(obj) | |
| json = ObjectSpace.dump(obj, output: :string) | |
| JSON.parse(json).fetch('shape_id') | |
| end | |
| def self.unshaped? = id.zero? | |
| def self.shaped? = !shaped? | |
| def self.of(obj) | |
| id = id(obj) | |
| lines = ObjectSpace.dump_shapes(output: :string) | |
| lines.each_line.lazy.filter_map do |line| | |
| shape = JSON.parse(line) | |
| shape if shape.fetch('id') == id | |
| end.first | |
| end | |
| end | |
| end | |
| lambdad = ->{} | |
| lambdad.instance_variable_set(:@tag, :tagged) | |
| pp RubyVM::Shape.of(lambdad) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment