Replace a specific line in a file using Ruby

Viewed 3956

I have a text file (a.txt) that looks like the following.

open
close
open
open
close
open

I need to find a way to replace the 3rd line with "close". I did some search and most method involve searching for the line than replace it. Can't really do it here since I don't want to turn all the "open" to "close".

Essentially (for this case) I'm looking for a write version of IO.readlines("./a.txt") [2].

2 Answers
Related