Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 15, 2025 19:38
Show Gist options
  • Select an option

  • Save havenwood/152081cf69f5f9e2de7dc93fbf0093d6 to your computer and use it in GitHub Desktop.

Select an option

Save havenwood/152081cf69f5f9e2de7dc93fbf0093d6 to your computer and use it in GitHub Desktop.
A nicer interface to look at the Shape of a Ruby Object
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