Not substitution \0, etc. in String.sub?

J

Jay McGavren

I have a string that happens to contain '\0', which I need to
substitute for a %x flag a la sprintf. Unfortunately, the '\0' gets
substituted for the initial match. So, this:

path = 'C:\Artist\Album\01 Title.mp3'
puts '%l'.sub(/%l/, path)

....gives me:

C:\Artist\Album%l1 Title.mp3

I'm sure there's some way to disable the substitution that doesn't
involve contortions with escaping the backslashes, but I don't know
what it is. Anyone?
 
E

eden li

Pass in a block instead:

puts '%l'.sub(/%l/) { path } #=> C:\Artist\Album\01 Title.mp3
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top