Number Lines in a File in Ruby
In this guide, you will learn how to add a number to the beginning of each line in a file to make it easier to locate content.
Guide Tasks
  • Read Tutorial
  • Complete the Exercise

Summary

Build a method that numbers each line of a file.

Exercise Description

For this exercise, you will need to define a method that will separate each line of the file in order to number the lines.

Example Data

Change:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur

Into:

1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
2. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
3. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur

Real World Usage

By numbering each line in a file, it helps finding certain information a lot easier and makes the task more efficient, which is what every company wants.

Test Cases

Code File