-3

ruby adding single \ to escape # instead of double backslash

for example, the # in string sd@# is preceded with single \ like this sd@\# automatically. I think it should add \\ (double backslash) to escape #.

Is it a bug in ruby syntax? if not is there a way to escape it manually

Please help

Stefan
  • 109,145
  • 14
  • 143
  • 218
Benison
  • 157
  • 1
  • 2
  • 17

1 Answers1

1

You can use a single \to escape #. Also, there is no need for escaping # in strings. Both mystring# and mystring\# will give output mystring#

Stefan
  • 109,145
  • 14
  • 143
  • 218
Albin Mathew
  • 21
  • 1
  • 4
  • apologies for not being clear, But the problem here is the string is being sent to the service as json: {"description"=>"sd@\#"} which is an invalid json – Benison Jan 08 '20 at 07:23
  • _"there is no need for escaping # in strings"_ – unless you want to suppress interpolation, e.g. `"#$$"` and `"\#$$"` give quite different results. – Stefan Jan 08 '20 at 16:02
  • Better you should use JSON.generate() if you want to pass it as json – Albin Mathew Jan 09 '20 at 11:42