gsub oddity?

M

Michael Neumann

Hi,

"\000".gsub("\000") { "\\000" }
# => "\\000"

"\000".gsub("\000", "\\000")
# => "\00000"

I expected both to return the same result.
Just tell me it's not a bug and I'm happy :)

Regards,

Michael
 
T

ts

M> Just tell me it's not a bug and I'm happy :)

This is not a bug :)

svg% ruby -e 'p "\000".gsub("\000", "\\&00")'
"\00000"
svg%



Guy Decoux
 
M

Mark Hubbart

Hi,

"\000".gsub("\000") { "\\000" }
# => "\\000"

"\000".gsub("\000", "\\000")
# => "\00000"

I expected both to return the same result.
Just tell me it's not a bug and I'm happy :)

Not a bug :)

When used in an argument rather than in a block, \0 inserts the matched
string. So gsub("\000", "\\000") is really saying "replace any null
characters with themselves followed by two zeros".

The block form does not interpolate backslashed matches.

cheers,
--Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Image on header 2
Langton's Ant 0
Z-Index/Drop-down menu issues 2
Flip-Cards with Local Images 1
Ruby/DL and errno 0
Regular expression contradiction 1
JQuery hover error 3
Need help to decode snmp string 2

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top