Try "a b c".sub("b", Regexp.escape("believe\\+me"))

2012/7/20 Albert Schlef <lists / ruby-forum.com>

> Rajarathnam p. wrote in post #1069363:
> > Please try the following code which works "a b c".sub("b",
> > 'believe\\\+me').
>
> Since this string isn't hardcoded, I'll have to write a function to add
> these slashes, and all this is becoming ugly: I don't want to waste time
> on debugging.
>
> I finally solved my problem by doing:
>
>   "a b c".sub("b") { "believe\\+me" }
>
> But I wonder if this is the "best" Ruby has to offer for my very humble
> demands.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>