# Gsub question

**URL:** <https://www.ruby-forum.com/t/gsub-question/92949>\
**Category:** Ruby\
**Created:** [April 26, 2007, 12:06am UTC](https://www.ruby-forum.com/t/gsub-question/92949 "2007-04-26T00:06:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 26, 2007, 12:06am UTC](https://www.ruby-forum.com/t/gsub-question/92949/1 "2007-04-26T00:06:36Z")

</div>

Hello,

i’m new to ruby, and working mostly with text.  
i have this very simple program that reads a file and replaces all ‘’  
with a whitespace. (rtf to txt conversion is causing an endless  
occurence of 's).  
the way i do it :

txt = "Messages posted to this group will make your email address  
visible to anyone on the Internet. "  
puts txt.gsub(’’ , ’ ')

produces an error:  
test.rb:1: unterminated string meets end of file

i’d like to know what i’m doing wrong, or is there another way of  
doing it properly.

thanks  
t

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 26, 2007, 12:18am UTC](https://www.ruby-forum.com/t/gsub-question/92949/2 "2007-04-26T00:18:37Z")

</div>

On 4/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the way i do it :
> 
> txt = "Messages posted to this group will make your email address  
> visible to anyone on the Internet. "  
> puts txt.gsub('' , ’ ')
> 
> produces an error:  
> test.rb:1: unterminated string meets end of file

Within a double quoted string (like your txt) the backslash character  
() sets up an escape sequence; so the trailing " is interpreted as  
an embedded quote mark. You need to use two backslashes to get the  
effect that you’re after, e.g.

```
txt = "Messages posted to this group... on the Internet. \\"
puts txt.gsub('\\', '')

```

For more info, see the “Strings” section of this chapter from  
Programming Ruby:

```
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html

```

Hope this helps,

Lyle

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 26, 2007, 12:21am UTC](https://www.ruby-forum.com/t/gsub-question/92949/3 "2007-04-26T00:21:37Z")

</div>

The slash in the txt variable should be escaped

The escaped version is as below

txt = “Messages posted to this group will make your email address  
visible to anyone on the Internet. \”  
puts txt.gsub(’\’ , ’ ')

Of course if you are reading the content directly from a file you  
need not escape the slashes explicitly

gg.txt

```auto
Messages posted to this group will make your email address
visible to anyone on the Internet. \

gg.rb

```

string = File.read(“gg.csv”)  
p string.gsub("\", “”)

Cheers,  
Ganesh G…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 26, 2007, 12:26am UTC](https://www.ruby-forum.com/t/gsub-question/92949/4 "2007-04-26T00:26:38Z")

</div>

Newbie taking a stab at it…  
On Apr 25, 2007, at 6:05 PM, removed\_email\_address@domain.invalid wrote:

> Hello,  
> Hello

> txt = "Messages posted to this group will make your email address  
> visible to anyone on the Internet. "  
> puts txt.gsub(’’ , ’ ')

> produces an error:  
> test.rb:1: unterminated string meets end of file  
> And right it should! You probably already know, but \ is the escape  
> thing. It basically means ‘disregard this next character’. So in  
> fact, that string never ends. A good text editor with coloring would  
> show that.

But your gsub method looks right 🙂

HTH  
---------------------------------------------------------------|  
~Ari  
“I don’t suffer from insanity. I enjoy every minute of it” --1337est  
man alive

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 26, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/gsub-question/92949/5 "2007-04-26T16:25:45Z")

</div>

Thanks All,

Q for Ganesh, was your file extended with .csv or is a text handled  
better this way.(are comma separated values an array)?

> p string.gsub("\", “”)

i don’t quite understand ‘p string.gsub’. Better do some reading.

what is the difference between a " and a ’ when dealing with text?

Thanks Again  
t
