class YARP::Source

This represents a source of Ruby code that has been parsed. It is used in conjunction with locations to allow them to resolve line numbers and source ranges.

Attributes

offsets[R]
source[R]

Public Class Methods

new(source, offsets = compute_offsets(source)) click to toggle source
# File yarp.rb, line 10
def initialize(source, offsets = compute_offsets(source))
  @source = source
  @offsets = offsets
end

Public Instance Methods

column(value) click to toggle source
# File yarp.rb, line 27
def column(value)
  value - offsets[line(value) - 1]
end
line(value) click to toggle source
# File yarp.rb, line 19
def line(value)
  offsets.bsearch_index { |offset| offset > value } || offsets.length
end
line_offset(value) click to toggle source
# File yarp.rb, line 23
def line_offset(value)
  offsets[line(value) - 1]
end
slice(offset, length) click to toggle source
# File yarp.rb, line 15
def slice(offset, length)
  source.byteslice(offset, length)
end

Private Instance Methods

compute_offsets(code) click to toggle source
# File yarp.rb, line 33
def compute_offsets(code)
  offsets = [0]
  code.b.scan("\n") { offsets << $~.end(0) }
  offsets
end