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
Read

The source code that this source object represents.

Read

The line number where this source starts.

Read

The list of newline byte offsets in the source code.

Class Methods

Create a new source object with the given source code. This method should be used instead of ‘new` and it will return either a `Source` or a specialized and more performant `ASCIISource` if no multibyte characters are present in the source code.

Create a new source object with the given source code.

Instance Methods

Return the column number in characters for the given byte offset.

Return the character offset for the given byte offset.

Returns the column number in code units for the given encoding for the given byte offset.

Returns the offset from the start of the file for the given byte offset counting in code units for the given encoding.

This method is tested with UTF-8, UTF-16, and UTF-32. If there is the concept of code units that differs from the number of characters in other encodings, it is not captured here.

Return the column number for the given byte offset.

Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.

Binary search through the offsets to find the line number for the given byte offset.

Binary search through the offsets to find the line number for the given byte offset.

Returns the byte offset of the end of the line corresponding to the given byte offset.

Return the byte offset of the start of the line corresponding to the given byte offset.

Returns the lines of the source code as an array of strings.

Perform a byteslice on the source code using the given byte offset and byte length.