Ruby: Remove first line from a String

Got a huge string and just want to shift the first line ?

Usage

a = "xxx\nyyy"
first_line = a.remove_first_line!
a == "yyy"
first_line == "xxx"

Code

class String
  def remove_first_line!
    first_newline = (index("\n") || size - 1) + 1
    slice!(0, first_newline).sub("\n",'')
  end
end

Leave a comment