Converting a multi line string to an array in Ruby using line breaks as delimiters

This is one way to deal with blank lines:

string.split(/\n+/)

For example,

string = "P07091 MMCNEFFEG

P06870 IVGGWECEQHS

SP0A8M0 VVPVADVLQGR




P01019 VIHNESTCEQ"

string.split(/\n+/)
  #=> ["P07091 MMCNEFFEG", "P06870 IVGGWECEQHS",
  #    "SP0A8M0 VVPVADVLQGR", "P01019 VIHNESTCEQ"]

I like to use this as a pretty generic method for handling newlines and returns:

lines = string.split(/\n+|\r+/).reject(&:empty?)

string = "P07091 MMCNEFFEG

P06870 IVGGWECEQHS

SP0A8M0 VVPVADVLQGR

P01019 VIHNESTCEQ"

Using CSV::parse

require 'csv'

CSV.parse(string).flatten
# => ["P07091 MMCNEFFEG", "P06870 IVGGWECEQHS", "SP0A8M0 VVPVADVLQGR", "P01019 VIHNESTCEQ"]

Another way using String#each_line :-

ar = []
string.each_line { |line| ar << line.strip unless line == "\n" }
ar # => ["P07091 MMCNEFFEG", "P06870 IVGGWECEQHS", "SP0A8M0 VVPVADVLQGR", "P01019 VIHNESTCEQ"]