Ruby 4.1 リファレンスマニュアル

instance method Prism::Comment#location

location -> Prism::LocationRuby 3.3 から[permalink][rdoc][edit]

コメントのソースコード上の位置を表す Prism::Location を返します。コメントの文字列そのものは location.slice で取得できます。

require "prism"

comment = Prism.parse("# hello\n1 + 1").comments.first
p comment.location.start_line # => 1
p comment.location.slice      # => "# hello"