Escaping '

Hi,
I need to replace ' by \'. How can I do that?

Thanks

···

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

David Nguyen wrote:

Hi,
I need to replace ' by \'. How can I do that?

Thanks

This was harder than I thought...in "vi"(vim actually) I just had to:

1,$ s/'/\\'/g

But Ruby didn't seem to like that....

Anyway, for reasons unknown to me, this works:

puts "'hello'".gsub("'","\\\\'")

Cheers

John

···

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

# David Nguyen wrote:
# > I need to replace ' by \'. How can I do that?
# This was harder than I thought...in "vi"(vim actually) I just had to:
# 1,$ s/'/\\'/g
# But Ruby didn't seem to like that....
# Anyway, for reasons unknown to me, this works:

how i wish the behaviour would change.
search the archives. this is a faq :slight_smile:

# puts "'hello'".gsub("'","\\\\'")

if like me, you want to lessen the those fences :slight_smile:
try,

"'hello'".gsub("'") { "\\'" }
#=> "\\'hello\\'"

or

"'hello'".gsub "'", %q[\\\']
#=> "\\'hello\\'"

for obvious reason, my brain prefers the former

kind regards -botp

···

From: John Pritchard-williams [mailto:monojohnny@googlemail.com]

//how i wish the behaviour would change.
search the archives. this is a faq :)//

oops sorry (I did it again)...perhaps you could include the link to
where this is explained though... :slight_smile:

···

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