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

**URL:** <https://www.ruby-forum.com/t/gsub-n-n/158169>\
**Category:** Ruby\
**Created:** [January 20, 2009, 1:20am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169 "2009-01-20T01:20:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 1:20am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/1 "2009-01-20T01:20:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 1:53am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/2 "2009-01-20T01:53:22Z")

</div>

On Jan 19, 2009, at 7:19 PM, Simon T. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 2:18am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/3 "2009-01-20T02:18:09Z")

</div>

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

> On Jan 19, 2009, at 7:19 PM, Simon T. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 2:46am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/4 "2009-01-20T02:46:35Z")

</div>

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

> I did try your suggestion, but I get “Hello\nWorld”. Will keep
> 
> > World  
> > \1 and \2 can reference groups in the regexp) and becomes \n  
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
+1 513-295-4739  
Skype: rob.biedenharn

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 8:50am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/5 "2009-01-20T08:50:29Z")

</div>

Simon T. wrote:  
If you want to substitute  
Hello\nWorld by  
Hello  
World  
you should try to work with single quotes:  
a = ‘Hello\nWorld’

Simon T. 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

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 5:08am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/6 "2009-01-20T05:08:08Z")

</div>

Hi,

Am Dienstag, 20. Jan 2009, 09:19:02 +0900 schrieb Simon T.:

> 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

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/7 "2009-01-20T22:50:26Z")

</div>

Simon T. 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.

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 25, 2009, 1:27am UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/8 "2009-01-25T01:27:05Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 26, 2009, 1:08pm UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/9 "2009-01-26T13:08:21Z")

</div>

On Jan 24, 2009, at 7:25 PM, Simon T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).

LOL!

---

<div class="post-metadata">

**Author:** ![simon1tan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@simon1tan](https://www.ruby-forum.com/u/simon1tan)\
**Post date:** [January 20, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/gsub-n-n/158169/10 "2009-01-20T19:17:54Z")

</div>

Simon T. 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
