Class: Prism::MagicComment

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/parse_result.rb,
ext/prism/extension.c

Overview

This represents a magic comment that was encountered during parsing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_loc, value_loc) ⇒ MagicComment

Create a new magic comment object with the given key and value locations.



433
434
435
436
# File 'lib/prism/parse_result.rb', line 433

def initialize(key_loc, value_loc)
  @key_loc = key_loc
  @value_loc = value_loc
end

Instance Attribute Details

#key_locObject (readonly)

A Location object representing the location of the key in the source.



427
428
429
# File 'lib/prism/parse_result.rb', line 427

def key_loc
  @key_loc
end

#value_locObject (readonly)

A Location object representing the location of the value in the source.



430
431
432
# File 'lib/prism/parse_result.rb', line 430

def value_loc
  @value_loc
end

Instance Method Details

#deconstruct_keys(keys) ⇒ Object

Implement the hash pattern matching interface for MagicComment.



449
450
451
# File 'lib/prism/parse_result.rb', line 449

def deconstruct_keys(keys)
  { key_loc: key_loc, value_loc: value_loc }
end

#inspectObject

Returns a string representation of this magic comment.



454
455
456
# File 'lib/prism/parse_result.rb', line 454

def inspect
  "#<Prism::MagicComment @key=#{key.inspect} @value=#{value.inspect}>"
end

#keyObject

Returns the key of the magic comment by slicing it from the source code.



439
440
441
# File 'lib/prism/parse_result.rb', line 439

def key
  key_loc.slice
end

#valueObject

Returns the value of the magic comment by slicing it from the source code.



444
445
446
# File 'lib/prism/parse_result.rb', line 444

def value
  value_loc.slice
end