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


#1

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?


#2

Pass in a block instead:

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