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


#1

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!


#2

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.


#3

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/

Under:

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


“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!


#4

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

#5

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 wrote:

"Project contacts:


Rails mailing list
removed_email_address@domain.invalid
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

#6

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


#7

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


“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!


#8

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

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


#9

Looks nice TNX!

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

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


“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!


#10

‘\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


#11

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


“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!


#12

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


Rails mailing list
removed_email_address@domain.invalid
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!


#13

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


#14

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

#15

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


“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!