Method: RSpec::Core::Formatters::HtmlSnippetExtractor#lines_around

Defined in:
lib/rspec/core/formatters/html_snippet_extractor.rb

#lines_around(file, line) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract lines of code centered around a particular line within a source file.

Parameters:

  • file (String)

    filename

  • line (Fixnum)

    line number

Returns:

  • (String)

    lines around the target line within the file (2 above and 1 below).



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec/core/formatters/html_snippet_extractor.rb', line 84

def lines_around(file, line)
  if File.file?(file)
    lines = File.read(file).split("\n")
    min = [0, line - 3].max
    max = [line + 1, lines.length - 1].min
    selected_lines = []
    selected_lines.join("\n")
    lines[min..max].join("\n")
  else
    "# Couldn't get snippet for #{file}"
  end
rescue SecurityError
  # :nocov: - SecurityError is no longer produced starting in ruby 2.7
  "# Couldn't get snippet for #{file}"
  # :nocov:
end