Trim blank newlines from string in Ruby

ruby, trim

Solution

The Ruby (and slightly less Perl-ish) way:

new_str = str.delete "\n"

...or if you want to do it in-place:

str.delete! "\n"

Problem

I have a string of four blank lines which all up makes eight lines in total in the following: ``` str = "aaa\n\n\nbbb\n\nccc\ddd\n" ``` I want to return this all in one line. The output should be like this on a single line: ``` aaabbbcccddd ``` I used various trim functions to get the output but still I am failing. What method do I have to use here?

Original source