I have a bunch of strings, and I'm trying to replace the spaces between them with newlines, using gsub:
<%= "string string string".gsub(" ","\n") %>
But for some reason, it just prints as:
string string string
What am I doing wrong?
NOTE: It prints as expected in IRB terminal. I'm only experiencing this problem in my browser, printing with ERB.
This works fine for me:
puts "string string string".gsub(" ","\n")
Maybe you have a situation similar to this:
x = "string string string"
y = x.gsub(" ","\n")
puts x
In line 2 the spaces are replaced but x
is not modified . The result y
has replaced spaces.
To replace the values directly in x
you must use gsub!
.
Based on your edit:
\\n
is not interpreted inside HTML, there you need a <br>
. The br-tag may not be masked so in the end you can try something like this inside ERB:
x.gsub(" ","<br/>\n").html_safe
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.