Substituting apostrphe's

Hi,

I have a bit of a problem, I can’t seem to substitute an apostrphe
successfully

a = "’"
a.gsub!(/’/,’\’’)
puts a

gives me nothing.

two backslash gives me syntax error.

one backslash gives me just the apostrophy.

How do I do this?

db

···


A.D. 1844: Samuel Morse invents Morse code. Cryptography export
restrictions prevent the telegraph’s use outside the U.S. and Canada.

Backspace the apostrophe.

a = “'”
=> “'”
a.gsub!(/'/, “")
=> "

Cheers,
Daniel.

···

On Wed, Dec 17, 2003 at 04:44:36AM +0900, Daniel Bretoi wrote:

Hi,

I have a bit of a problem, I can’t seem to substitute an apostrphe
successfully

a = “'”
a.gsub!(/‘/,’\'')
puts a

gives me nothing.

two backslash gives me syntax error.

one backslash gives me just the apostrophy.

How do I do this?

db


A.D. 1844: Samuel Morse invents Morse code. Cryptography export
restrictions prevent the telegraph’s use outside the U.S. and Canada.


Daniel Carrera | “Software is like sex. It’s better when it’s free”.
PhD student. |
Math Dept. UMD | – Linus Torvalds

I have a bit of a problem, I can’t seem to substitute an apostrphe
successfully

a = “'”
a.gsub!(/‘/,’\'')
puts a

gives me nothing.

two backslash gives me syntax error.

one backslash gives me just the apostrophy.

How do I do this?

irb(main):003:0> a = “a”
=> “a”
irb(main):004:0> a.gsub(/a/, ‘'’)
=> “'”
irb(main):007:0> a = “abc”
=> “abc”
irb(main):008:0> a.gsub(/a/, ‘\'’)

so ' gives you the apostrophe, \' gives you the part of the string
after the match. So three slashes gives nothing because the part of the
string after the match is empty. One slash gives you just the apostrophe
because you substitute the apostrophe with a brand new one. But I can’t
say from your code what you want it to do.

Peter

puts a.gsub(/‘/) { "\’" }

-austin

···

On Wed, 17 Dec 2003 04:44:36 +0900, Daniel Bretoi wrote:

a = “'”
a.gsub!(/‘/,’\'')
puts a


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.16
* 15.50.34

irb(main):003:0> a = “a”
=> “a”
irb(main):004:0> a.gsub(/a/, ‘'’)
=> “'”
irb(main):007:0> a = “abc”
=> “abc”
irb(main):008:0> a.gsub(/a/, ‘\'’)

Did it again… The last line gives “bcbc” as result.

Peter

In article Pine.LNX.4.44.0312162108160.3096-100000@merlin.cs.kuleuven.ac.be,

irb(main):003:0> a = “a”
=> “a”
irb(main):004:0> a.gsub(/a/, ‘'’)
=> “'”
irb(main):007:0> a = “abc”
=> “abc”
irb(main):008:0> a.gsub(/a/, ‘\'’)

Did it again… The last line gives “bcbc” as result.

What do you want it to do?

You may be suffering from some horrid perl remnants, as $’ is the
post-match variable, and in the perl debugger:

DB<1> $s = ‘abc’

DB<2> $s =~ s/a/$'/g

DB<3> print $s
bcbc

I postulate that because ri says this about String#gsub

------------------------------------------------------------ String#gsub
str.gsub( pattern, replacement ) → aString
str.gsub( pattern ) {| match | block } → aString

···

Peter Peter.Vanbroekhoven@cs.kuleuven.ac.be wrote:

 Returns a copy of str with all occurrences of pattern replaced with
 either replacement or the value of the block. The pattern will
 typically be a Regexp; if it is a String then no regular expression
 metacharacters will be interpreted (that is /\d/ will match a
 digit, but '\d' will match a backslash followed by a 'd').
 If a string is used as the replacement, special variables from the
 match (such as $& and $1) cannot be substituted into it, as
 substitution into the string occurs before the pattern match
 starts. However, the sequences \1, \2, and so on may be used to
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 interpolate successive groups in the match. These sequences are
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 shown in Table ?? on page ??.
 In the block form, the current match is passed in as a parameter,
 and variables such as $1, $2, $`, $&, and $' will be set
 appropriately. The value returned by the block will be substituted
 for the match on each call.
 The result inherits any tainting in the original string or any
 supplied replacement string.
    "hello".gsub(/[aeiou]/, '*')              #=> "h*ll*"
    "hello".gsub(/([aeiou])/, '<\1>')         #=> "h<e>ll<o>"
    "hello".gsub(/./) {|s| s[0].to_s + ' '}   #=> "104 101 108 108 111 "

Maybe you want to avoid the \ altogether

$ irb --simple-prompt

‘abc’.gsub(/a/, “'”)
=> “'bc”
‘abc’.gsub(/a/) { “'” }
=> “'bc”

or you want to include a \ in the replacement

puts ‘abc’.gsub(/a/, “\\'”)
'bc
nil
puts ‘abc’.gsub(/a/) { “\'” }
'bc
=> nil

Hope this helps,

Mike

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA