Skip to content

Instantly share code, notes, and snippets.

@kddnewton
Last active August 28, 2023 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kddnewton/7ca67761106130a5ea537ed50e961afa to your computer and use it in GitHub Desktop.
Save kddnewton/7ca67761106130a5ea537ed50e961afa to your computer and use it in GitHub Desktop.
Fetch YARP AST for a method
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