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

class Prism::Comment

[edit]

要約

Prism::ParseResult#commentsPrism?.parse_comments で得られる、ソースコード中のコメントを表す抽象基底クラスです。

実際に生成されるのはサブクラスの Prism::InlineComment(# から始まる通常のコメント)または Prism::EmbDocComment(=begin=end の埋め込みドキュメント)のどちらかで、このクラス自体のインスタンスが返されることはありません。

[SEE_ALSO] Prism::ParseResult#comments, Prism?.parse_comments

目次

インスタンスメソッド

インスタンスメソッド

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"
slice -> StringRuby 3.4 から[permalink][rdoc][edit]

コメントの文字列そのものを返します。location.slice の短縮形です。

require "prism"

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