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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top