f



Gsub!("\n","\n")

Hi all, I'm not sure why but I assumed gsub! would allow me to replace
\n with line feeds or \r for carriage returns.

How do I gsub! and replace everything with a nonvisible character like
newline? I was using this results.gsub!("\n","\n") but obviously that
does not work :( also tried results.gsub!("\n",10.chr). No go.

Kinda of frustrating for something so simple.  I guess I could always do
some recursion.  Thanks.
-- 
Posted via http://www.ruby-forum.com/.

0
simon1tan (4)
1/20/2009 12:19:02 AM
comp.lang.ruby 48885 articles. 0 followers. Post Follow

9 Replies
702 Views

Similar Articles

[PageSpeed] 45

On Jan 19, 2009, at 7:19 PM, Simon Tan wrote:

> Hi all, I'm not sure why but I assumed gsub! would allow me to replace
> \n with line feeds or \r for carriage returns.
>
> How do I gsub! and replace everything with a nonvisible character like
> newline? I was using this results.gsub!("\n","\n") but obviously that
> does not work :( also tried results.gsub!("\n",10.chr). No go.
>
> Kinda of frustrating for something so simple.  I guess I could  
> always do
> some recursion.  Thanks.
> -- 


Are you trying to turn this:

Hello,
World

Into something like:

Hello,\nWorld

If so, you need to change "\n" (1 character string) into "\\\\n" (3  
character string, two escaped backslashs and the letter 'n').  The  
replacement string will undergo two different interpretations: as a  
string literal "\\\\n" becomes \\n which when interpreted as a  
replacement for a regexp undergoes another round (so that things like  
\1 and \2 can reference groups in the regexp) and becomes \n

Yes, this is tricky, but it's because there are two levels of escaping  
going on and both use the backslash.

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com



0
Rob7461 (594)
1/20/2009 12:51:13 AM
haha actually, the other way around :)  I have a string(I believe...I'm 
using Hpricot to retreive web pages and it likes to add slashes to 
everything). For example, I want to change "Hello, \nWorld" by changing 
\n into a newline.  So the output should be like:
Hello
World


I did try your suggestion, but I get "Hello\\nWorld".  Will keep playing 
with the slashes.

Rob Biedenharn wrote:
> On Jan 19, 2009, at 7:19 PM, Simon Tan wrote:
> 
>> -- 
> Are you trying to turn this:
> 
> Hello,
> World
> 
> Into something like:
> 
> Hello,\nWorld
> 
> If so, you need to change "\n" (1 character string) into "\\\\n" (3
> character string, two escaped backslashs and the letter 'n').  The
> replacement string will undergo two different interpretations: as a
> string literal "\\\\n" becomes \\n which when interpreted as a
> replacement for a regexp undergoes another round (so that things like
> \1 and \2 can reference groups in the regexp) and becomes \n
> 
> Yes, this is tricky, but it's because there are two levels of escaping
> going on and both use the backslash.
> 
> -Rob
> 
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com

-- 
Posted via http://www.ruby-forum.com/.

0
simon1tan (4)
1/20/2009 1:16:39 AM
Are you seeing something like:
irb> look = 'here is a " and I\'ll put a \' here, too'
=> "here is a \" and I'll put a ' here, too"
irb> puts look
here is a " and I'll put a ' here, too
=> nil

The rules for what needs to be escaped depend on the kind of quotes  
used.

Are you seeing output that is shown in double quotes to indicate that  
it is a string?

-Rob

On Jan 19, 2009, at 8:16 PM, Simon Tan wrote:

> haha actually, the other way around :)  I have a string(I  
> believe...I'm
> using Hpricot to retreive web pages and it likes to add slashes to
> everything). For example, I want to change "Hello, \nWorld" by  
> changing
> \n into a newline.  So the output should be like:
> Hello
> World
>
>
> I did try your suggestion, but I get "Hello\\nWorld".  Will keep  
> playing
> with the slashes.
>
> Rob Biedenharn wrote:
>> On Jan 19, 2009, at 7:19 PM, Simon Tan wrote:
>>
>>> -- 
>> Are you trying to turn this:
>>
>> Hello,
>> World
>>
>> Into something like:
>>
>> Hello,\nWorld
>>
>> If so, you need to change "\n" (1 character string) into "\\\\n" (3
>> character string, two escaped backslashs and the letter 'n').  The
>> replacement string will undergo two different interpretations: as a
>> string literal "\\\\n" becomes \\n which when interpreted as a
>> replacement for a regexp undergoes another round (so that things like
>> \1 and \2 can reference groups in the regexp) and becomes \n
>>
>> Yes, this is tricky, but it's because there are two levels of  
>> escaping
>> going on and both use the backslash.
>>
>> -Rob
>>
>> Rob Biedenharn    http://agileconsultingllc.com
>> Rob@AgileConsultingLLC.com
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

Rob Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
+1 513-295-4739
Skype:	rob.biedenharn



0
Rob7461 (594)
1/20/2009 1:44:17 AM
Hi,

Am Dienstag, 20. Jan 2009, 09:19:02 +0900 schrieb Simon Tan:
> Hi all, I'm not sure why but I assumed gsub! would allow me to replace
> \n with line feeds or \r for carriage returns.

  str.gsub! "\\n", "\n"
  str.gsub! "\\r", "\r"

This is a security risk:

  eval %Q("#{str}")

Maybe this does:

  str = "hello\\nbye"
  str.gsub! /\\(\w)/ do |m| eval '"\\' + $1 + '"' end

Okay, \ escaping is a weird matter.

You know that gsub! returns nil in case nothing was changed?

Bertram


-- 
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

0
lists2051 (430)
1/20/2009 4:03:36 AM
Simon Tan wrote:
If you want to substitute
Hello\nWorld by
Hello 
World 
you should try to work with single quotes:
 a = 'Hello\nWorld'

Simon Tan wrote:
=> "Hello\\nWorld"
irb(main):002:0> puts a
Hello\nWorld
=> nil
irb(main):003:0> a.gsub!('\n',"\n")
=> "Hello\nWorld"
irb(main):004:0> puts a
Hello
World

> Hi all, I'm not sure why but I assumed gsub! would allow me to replace
> \n with line feeds or \r for carriage returns.
> 
> How do I gsub! and replace everything with a nonvisible character like
> newline? I was using this results.gsub!("\n","\n") but obviously that
> does not work :( also tried results.gsub!("\n",10.chr). No go.
> 
> Kinda of frustrating for something so simple.  I guess I could always do
> some recursion.  Thanks.

0
tammo (19)
1/20/2009 7:46:25 AM
Simon Tan wrote:
> I'm not sure why but I assumed gsub! would allow me to replace
> \n with line feeds

"\n" *is* a line feed.
>> string = "hello\nworld"
=> "hello\nworld"
>> puts string
hello
world
=> nil

HTH,
Sebastian
-- 
NP: Dire Straits - Brothers In Arms
Jabber: sepp2k@jabber.org
ICQ: 205544826

0
sepp2k (592)
1/20/2009 4:32:37 PM
Simon Tan wrote:
> I have a string(I believe...I'm 
> using Hpricot to retreive web pages and it likes to add slashes to 
> everything). For example, I want to change "Hello, \nWorld" by changing 
> \n into a newline.

Perhaps there's a misunderstanding here. I doubt Hpricot adds anything. 
If you see this "Hello, \nWorld" in 'irb' then that's because 'irb' 
shows you the string in the way you'd write it in your source-code. The 
two characters "\n" are actually a newline.
-- 
Posted via http://www.ruby-forum.com/.

0
albertschlef (195)
1/20/2009 9:48:52 PM
Ok, I just realized I've been using p to print everything out!  "Puts" 
works a whole lot better.  Doh!  Thanks for all the help.  Sorry for 
phrasing the question poorly :)

-- 
Posted via http://www.ruby-forum.com/.

0
simon1tan (4)
1/25/2009 12:25:05 AM



On Jan 24, 2009, at 7:25 PM, Simon Tan <simon1tan@yahoo.com> wrote:

> Ok, I just realized I've been using p to print everything out!  "Puts"
> works a whole lot better.  Doh!  Thanks for all the help.  Sorry for
> phrasing the question poorly :)
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

LOL!

0
list.rb (49)
1/26/2009 12:05:42 PM
Reply: