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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6bbdb1e4c7e8af30ca469e57f9e7d739?d=identicon&s=25 Jay McGavren (Guest)
on 2007-04-15 07: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)

...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?
8bc543795b502900b5333aea73ad5533?d=identicon&s=25 Eden Li (edenli)
on 2007-04-15 08: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.