I have a problem with saving records in MongoDB using Mongoid when they contain multibyte characters. This is the string:
a="Chris \xA5\xEB\xAE\xDFe\xA5"
I first convert it to BINARY and I then gsub it like this:
a.force_encoding("BINARY").gsub(0xA5.chr,"oo")
…which works fine:
=> "Chris oo\xEB\xAE\xDFeoo"
But it seems that I can not use the chr method if I use Regexp:
a.force_encoding("BINARY").gsub(/0x....?/.chr,"")
NoMethodError: undefined method `chr' for /0x....?/:Regexp
Anybody with the same issue?
Thanks a lot…
You can do that with interpolation
gives
EDIT: based on the comments, here a version that translates the binary encode string to an ascii representation and do a regex on that string
the [2..-3] at the end is to get rid of the beginning [” and and trailing “]
NOTE: to just get rid of the special characters you also could just use