Last active
August 28, 2023 18:32
-
-
Save kddnewton/7ca67761106130a5ea537ed50e961afa to your computer and use it in GitHub Desktop.
Fetch YARP AST for a method
This file contains 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 "yarp" | |
module YARPAST | |
def yarp_ast | |
ast = | |
begin | |
RubyVM::AbstractSyntaxTree.of(self, keep_script_lines: true) | |
rescue ArgumentError | |
end | |
return unless ast | |
lines = ast.script_lines | |
source = lines[(ast.first_lineno - 1)..(ast.last_lineno - 1)] | |
source[-1] = source[-1].byteslice(...ast.last_column) | |
source[0] = source[0].byteslice(ast.first_column..) | |
YARP.parse(source.join) | |
end | |
Method.include(self) | |
UnboundMethod.include(self) | |
end | |
pp YARP::Location.method(:null).yarp_ast |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment