Hi,
Here’s some behaviour I don’t immediately understand:
irb(main):119:0> foo = “'123”
=> "‘123"
irb(main):120:0> foo.gsub(/’/, “\’”)
=> “123123”
Why does the apostrophe get replaced with a copy of the text that
follows it?
I’m trying to replace every instance of ’ with ’ but it’s proving very
tricky. Nothing I can come up with seems to work, no matter how many
backslashes I use, whether I use single or double quotes, whether or not
I use gsub with a block, etc.
This should be so easy, but it has me stumped.
Ian
···
–
Ian Macdonald | It is better to live rich than to die rich.
System Administrator | – Samuel Johnson
ian@caliban.org |
http://www.caliban.org |
>
Hi –
Hi,
Here’s some behaviour I don’t immediately understand:
irb(main):119:0> foo = “'123”
=> “'123”
irb(main):120:0> foo.gsub(/‘/, "\’")
=> “123123”
Why does the apostrophe get replaced with a copy of the text that
follows it?
What you’re hitting is a special meaning of ' – namely, the
post-match part of the match. It’s similar to the $`, $&, $’
variables (pre-match, match, post-match).
I’m trying to replace every instance of ’ with ' but it’s proving very
tricky. Nothing I can come up with seems to work, no matter how many
backslashes I use, whether I use single or double quotes, whether or not
I use gsub with a block, etc.
Try this:
“'123”.gsub(/‘/, "\\’")
David
···
On Fri, 30 May 2003, Ian Macdonald wrote:
–
David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
> irb(main):119:0> foo = "'123"
> => "'123"
> irb(main):120:0> foo.gsub(/'/, "\\'")
> => "123123"
>
> Why does the apostrophe get replaced with a copy of the text that
> follows it?
’ has special meaning (“after the match”, iirc, which makes sense here).
A string “” requires you to escape , as \, so:
“\’” => ’ => after-the-match substitution
Try:
foo.gsub /’/, “\\’” => “\'123”
(Remember that “\’” is ', because \ is escaped.) This double-escapes
the backslash for the results you want. (Try printing the result when
you get it.)
hth,
···
On Fri, 30 May 2003 06:58:35 +0900 Ian Macdonald ian@caliban.org wrote:
–
Ryan Pavlik rpav@users.sf.net
“Spinal hazards are hazardous…” - 8BT
foo = “'123”
puts foo.gsub(/‘/) {’\‘+"’"}
I find it more readable than the multiply-self-escaping string of
backslashes.
martin
···
Ian Macdonald ian@caliban.org wrote:
I’m trying to replace every instance of ’ with ' but it’s proving very
tricky. Nothing I can come up with seems to work, no matter how many
backslashes I use, whether I use single or double quotes, whether or not
I use gsub with a block, etc.
This should be so easy, but it has me stumped.
Thanks. I couldn’t see why ' might be getting expanded like that, as I
didn’t see it as being analogous to $’
This works for me, too, and is a little more readable to my eyes:
“'123”.gsub(/‘/) { %q(\’) }
Ian
···
On Fri 30 May 2003 at 07:04:35 +0900, dblack@superlink.net wrote:
On Fri, 30 May 2003, Ian Macdonald wrote:
Hi,
Here’s some behaviour I don’t immediately understand:
irb(main):119:0> foo = “'123”
=> “'123”
irb(main):120:0> foo.gsub(/‘/, "\’")
=> “123123”
Why does the apostrophe get replaced with a copy of the text that
follows it?
What you’re hitting is a special meaning of ' – namely, the
post-match part of the match. It’s similar to the $`, $&, $’
variables (pre-match, match, post-match).
I’m trying to replace every instance of ’ with ' but it’s proving very
tricky. Nothing I can come up with seems to work, no matter how many
backslashes I use, whether I use single or double quotes, whether or not
I use gsub with a block, etc.
Try this:
“'123”.gsub(/‘/, "\\’")
–
Ian Macdonald | If all the world’s economists were laid end
System Administrator | to end, we wouldn’t reach a conclusion.
ian@caliban.org | – William Baumol
http://www.caliban.org |
>
But ‘\’ and “\” are the same thing (a single backslash)
puts ‘\’.size #>> 1
puts “\”.size #>> 1
So concatenating two strings like that doesn’t help you at all.
Regards,
Brian.
···
On Fri, May 30, 2003 at 08:48:04PM +0900, Martin DeMello wrote:
Ian Macdonald ian@caliban.org wrote:
I’m trying to replace every instance of ’ with ' but it’s proving very
tricky. Nothing I can come up with seems to work, no matter how many
backslashes I use, whether I use single or double quotes, whether or not
I use gsub with a block, etc.
This should be so easy, but it has me stumped.
foo = “'123”
puts foo.gsub(/‘/) {’\‘+"’"}
I find it more readable than the multiply-self-escaping string of
backslashes.
foo = “'123”
puts foo.gsub(/‘/) {’\‘+"’"}
I find it more readable than the multiply-self-escaping string of
backslashes.
But ‘\’ and “\” are the same thing (a single backslash)
Yeah, that was simply a matter of defaulting to ‘’ for constant strings
unless I need a “'”
So concatenating two strings like that doesn’t help you at all.
Oops - so it doesn’t. Please to ignore that post. What did help is that
“\'” doesn’t seem to be magic in the {} form of gsub.
martin
···
Brian Candler B.Candler@pobox.com wrote:
On Fri, May 30, 2003 at 08:48:04PM +0900, Martin DeMello wrote: