# Going nuts on '\\n' to \<br /\>

**URL:** <https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681>\
**Category:** Rails\
**Created:** [January 17, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681 "2006-01-17T22:47:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 17, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/1 "2006-01-17T22:47:44Z")

</div>

Hi all,

Is there no way to easily resolve this. It can’t be that I’m the first  
that  
wants this, and it wouldn’t be RoR if it’s not possible.

On the mysql prompt with the select description from todos … linefeeds  
are  
show, so they are there.

This is the actual contents with a linefeed after the “:”

“Project contacts:  
delete table, point project to contacts”

But this: \<%= todo.description.tr(’\n’, ‘  
’) %\>

Results in this:

Project co  
(It actually replaces “n”,and it’s on to the next cell)

And with double quotes int the statement it’s this:

“Project contacts:”

(and it’s on to the next cell).

Tried RedCloth but that doesn’t work as simple either (not at all  
actually,  
because it doesn’t know textilize or textile as a methgod after install)

Help is greately appreciated.

Regards,

Gerard.

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 17, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/2 "2006-01-17T22:59:47Z")

</div>

Hi Gerard,

regarding RedCloth: sounds as if you didn’t make rails to know about  
RedCloth. Is

require ‘rubygems’  
require\_gem ‘RedCloth’

in your environment.rb?

whats .tr about (don’t know this method). You might have a try with  
todo.description.gsub(’\n’, ‘  
’)

Best Regards  
Jan P.

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 17, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/3 "2006-01-17T23:33:00Z")

</div>

Jan,

Found it after chewing on your input. This one does the trick. gsub  
should be  
written with slashes: \<%= todo.description.gsub(/\n/, ‘  
’) %\>\<

What still confuses me though is that redcloth is not available:

> require ‘rubygems’  
> require\_gem ‘RedCloth’  
> I put these in and restarted webrick. WIth the result:

undefined method `textile’ for “Project contacts:\ndelete table, point  
project  
to contacts”:String  
(textilize doesn’t work either)

> whats .tr about (don’t know this method). You might have a try with  
> todo.description.gsub(‘\n’, ‘  
> ’)  
> I’ve seen the “tr” come by somewhere in this list and is mentioned on  
> the ruby  
> book:

[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

Under:

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref\_c\_string.html#String.tr](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.tr)

Thanx a lot!

Regards,

Gerard.

> > On the mysql prompt with the select description from todos … linefeeds
> > 
> > actually, because it doesn’t know textilize or textile as a methgod after  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 18, 2006, 12:51am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/4 "2006-01-18T00:51:21Z")

</div>

On Tue, Jan 17, 2006 at 10:10:28PM +0100, Gerard P. wrote:

> (It actually replaces “n”,and it’s on to the next cell)  
> (Taking into account your later e-mails as well)

The problem isn’t that you need to feed gsub a regex (which is what /\n/  
actually gives you) but rather than single quotes don’t expand escape  
sequences like \n. What you really needed was gsub("\n", ‘  
’)  
instead,  
which gives you the correct result.

- Matt

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 18, 2006, 12:39am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/5 "2006-01-18T00:39:18Z")

</div>

String#tr is for translating single characters to other single  
characters. You can’t replace a single character with more than one  
characer using tr. Use String#gsub instead.

todo.description.gsub( “\n”, “  
” )

On 1/17/06, Gerard P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> "Project contacts:
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Mik Mifflin  
“Whether freedom is going to survive at all is in doubt, but we’ve got  
to try” - RMS

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 18, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/6 "2006-01-18T19:34:14Z")

</div>

Gerard:  
On Jan 17, 2006, at 1:10 PM, Gerard P. wrote:

> But this: \<%= todo.description.tr(‘\n’, ‘  
> ’) %\>

\<%= todo.description.gsub ‘\n’,‘  
’ %\> works on your example string.  
Cheers,  
Hasan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 1:24am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/7 "2006-01-19T01:24:25Z")

</div>

Hmm … so ‘\n’ would be expanded before used, that right?

GrtzG

On Wednesday 18 January 2006 00:11, Matthew P. tried to type  
something  
like:

> > Project co
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 18, 2006, 1:06am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/8 "2006-01-18T01:06:28Z")

</div>

I figured I would just add that you may want to use the  
“simple\_format” function - it doesn’t just do \n to   
 but a few  
other things and wraps in a

tag - not always desirable but since  
you were considering using Textile I figured it was worth mentioning.

Docs on simple\_format are here:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000425](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000425)

If you take a look at the source it shows you’ll see it’s some regexs,  
nothing fancy, but nevertheless useful.

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 1:27am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/9 "2006-01-19T01:27:27Z")

</div>

Looks nice TNX!

Although I got it working with gsub I’m still wondering why RedCloth is  
nagging me with unknown method errors … ☹

GrtzG

On Wednesday 18 January 2006 00:27, Jonathan D. tried to type  
something  
like:

> nothing fancy, but nevertheless useful.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 1:33am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/10 "2006-01-19T01:33:33Z")

</div>

‘\n’ is a two character string ‘\n’[0] =\> \ and ‘\n’[1] =\> n  
“\n” is a one character string

get your ascii chart and fire up irb!

irb(main):001:0\> ‘\n’[0]  
=\> 92  
irb(main):002:0\> ‘\n’[1]  
=\> 110  
irb(main):003:0\> “\n”[0]  
=\> 10  
irb(main):004:0\> “\n”[1]  
=\> nil  
irb(main):005:0\>

single-quoted strings don’t interpolate their contents, not even for  
-escapes

-Rob

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 1:36am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/11 "2006-01-19T01:36:36Z")

</div>

That’s weird,

it didn’t when used in the template. any idea?

GrtzG

On Wednesday 18 January 2006 18:55, Hasan D. tried to type something  
like:

> Gerard:
> 
> On Jan 17, 2006, at 1:10 PM, Gerard P. wrote:
> 
> > But this: \<%= todo.description.tr(‘\n’, ‘  
> > ’) %\>
> 
> \<%= todo.description.gsub ‘\n’,‘  
> ’ %\> works on your example string.  
> Cheers,  
> Hasan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 1:39am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/12 "2006-01-19T01:39:39Z")

</div>

I’ll hack on that thanks!

So those win cr/lf’s would contain two chars then when in between  
doubles?

GrtzG

On Thursday 19 January 2006 00:55, Rob B. tried to type  
something  
like:

> =\> 10  
> At 1/18/2006 06:45 PM, Gerard P. wrote:
> 
> > > > "Project contacts:
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 2:12am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/13 "2006-01-19T02:12:58Z")

</div>

At 1/18/2006 07:21 PM, Matt P. wrote:

> > > > 
> > > 
> > > instead, which gives you the correct result.
> 
> - Matt  
> \*nix or Windoze, doesn’t seem to matter here:

$ uname  
Linux  
$ irb  
irb(main):001:0\> ‘\n’.length  
=\> 2  
irb(main):002:0\> “\n”.length  
=\> 1  
irb(main):003:0\> “\n”[0]  
=\> 10  
irb(main):004:0\>

Microsoft Windows XP [Version 5.1.2600]  
© Copyright 1985-2001 Microsoft Corp.

C:\>irb  
irb(main):001:0\> ‘\n’.length  
=\> 2  
irb(main):002:0\> “\n”.length  
=\> 1  
irb(main):003:0\> “\n”[0]  
=\> 10  
irb(main):004:0\>

-Rob

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 2:00am UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/14 "2006-01-19T02:00:49Z")

</div>

On Thu, Jan 19, 2006 at 12:45:57AM +0100, Gerard P. wrote:

> > > Results in this:
> 
> Hmm … so ‘\n’ would be expanded before used, that right?

No, ‘\n’ is a two character String object – a literal backslash  
followed by  
a literal lowercase n. “\n” is a single character String object,  
containing  
the newline character ASCII code 0x0A. (On windows, “\n” _may_ expand  
to  
the two-character String object 0x0D 0x0A under some circumstances, but  
I’ve  
got no experience with Ruby on windows, so if you’re on windows you’re  
on  
your own).

- Matt

---

<div class="post-metadata">

**Author:** ![gerardjp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gerardjp](https://www.ruby-forum.com/u/gerardjp)\
**Post date:** [January 19, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/going-nuts-on-n-to-br/52681/15 "2006-01-19T22:26:21Z")

</div>

No windows for me thanx.

I was just curious.

TNX and Grtz

Gerard.

On Thursday 19 January 2006 01:21, Matt P. tried to type something  
like:

> > > > 
> > > 
> > > /\n/ actually gives you) but rather than single quotes don’t expand  
> > > you’re on windows you’re on your own).
> 
> - Matt
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!
