Forum: Ruby Not substitution \0, etc. in String.sub?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jay McGavren (Guest)
on 2007-04-15 09:41
(Received via mailing list)
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) 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?
Eden L. (Guest)
on 2007-04-15 10:54
(Received via mailing list)
Pass in a block instead:

puts '%l'.sub(/%l/) { path } #=> C:\Artist\Album\01 Title.mp3
This topic is locked and can not be replied to.