Retrieving Adjacent Lines

In this lesson, we are going to experiment with lines instead of words. Specifically, we are going to work on implementing a new helper method that will accept a value, a target line number, a radius, and a number of additional lines surrounding our target line to return. Our new method will behave similarly to the excerpt helper method but work on lines instead.

Let’s take a look at the following piece of text:

Line one
Line two
Line three
Line four
Line five
Line six
Line seven
Line eight
Line nine
Line ten

Implementation of thelineExcerpt macro method

Suppose we want to extract line five and two additional lines on either side and return them as an array. The basic implementation of this, while accounting for the radius lines exceeding the number of total lines, might look similar to the following:

Get hands-on with 1400+ tech skills courses.