Array.join("\n") not the way to join with a newline?

Viewed 108190

I have an array..

[1,2,3,4]

and I want a string containing all the elements separated by a newline..

1

2

3

4

but when I try [1,2,3,4].join("\n") I get

1\n2\n3\n4

I feel like there is an obvious answer but I can't find it!

7 Answers

As some of the other answers above imply, Rails may be escaping your code before rendering as html. Here's a sample that addresses this problem (first sanitizing the inputs, so that you can "safely" call html_safe on the result):

my_array = [1, 2, 3, 4]
my_array.map{ |i| i.to_s.sanitize }.join("\n").html_safe

You only need sanitize if you don't trust the inputs.

Related