Ruby: Read A File With One Line Of Code

Posted by luca
on Tuesday, November 27

This snippet shows how to read a file and puts all the lines into an array.

Explanation

The open method returns an IO object, that include the Enumerable module, now we can just use #map (or #collect).

The splat operator is only a sugar syntactical shortcut for map

.