Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

ErrorHighlight.spot

spot(obj, **opts)

Identify the code fragment where a given exception occurred.

Options:

point_type: :name | :args

:name (default) points to the method/variable name where the exception occurred.
:args points to the arguments of the method call where the exception occurred.

backtrace_location: Thread::Backtrace::Location

It locates the code fragment of the given backtrace_location.
By default, it uses the first frame of backtrace_locations of the given exception.

Returns:

Example
{
  first_lineno: Integer,
  first_column: Integer,
  last_lineno: Integer,
  last_column: Integer,
  snippet: String,
  script_lines: [String],
} | nil

Limitations:

Currently, ErrorHighlight.spot only supports a single-line code fragment. Therefore, if the return value is not nil, first_lineno and last_lineno will have the same value. If the relevant code fragment spans multiple lines (e.g., Array#[] of ary[(newline)expr(newline)]), the method will return nil. This restriction may be removed in the future.

See also .prism_find .prism also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1