How to split string into paragraphs using first comma? How to split string into paragraphs using first comma? ruby ruby

How to split string into paragraphs using first comma?


String#split has a second argument, the maximum number of fields returned in the result array:http://ruby-doc.org/core/classes/String.html#M001165

@address.split(",", 2) will return an array with two strings, split at the first occurrence of ",".

the rest of it is simply building the string using interpolation or if you want to have it more generic, a combination of Array#map and #join for example

@address.split(",", 2).map {|split| "<p>#{split}</p>" }.join("\n")


break_at = @address.index(",") + 1result = "<p>#{@address[0, break_at]}</p><p>#{@address[break_at..-1].strip}</p>"


rather:

break_at = @address.index(", ")result = "<p>#{@address[0, break_at+1]}</p><p>#{@address[break_at+1..-1]}</p>"