24

I'm getting two errors, both revolving around encoding and both related.

The first error (technically, a warning) I get when starting up WEBrick:

/Users/USERNAME/example/config/initializers/bb-ruby.rb:54: warning: invalid Unicode Property \P: /\:\-?\P/

The line it's referring to is: /\:\-?\P/,

It's just a bit of regex, ultimately part of this block:

@@tags['Razzing'] = [
  /\:\-?\P/,
  '<img src="/assets/emoticons/razzing.png">',
  'Razzing',
  ':P',
  :razzing]

Then, I also get the following error when parsing some strings (presumably due to this same line)...

Encoding::CompatibilityError
incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

I'm running Ruby 1.9.2 and Rails 3.2.1.

2 Answers 2

27

Your Regex is being "compiled" as ASCII-8BIT.

Just add the encoding declaration at the top of the file where the Regex is declared:

# encoding: utf-8

And you're done. Now, when Ruby is parsing your code, it will assume every literal you use (Regex, String, etc) is specified in UTF-8 encoding.

UPDATE: UTF-8 is now the default encoding for Ruby 2.0 and beyond.

Sign up to request clarification or add additional context in comments.

7 Comments

That stopped the Encoding::CompatibilityError error, but I'm still getting the invalid Unicode Property warning when starting up Webrick.
The Regex engine changed on Ruby 1.9… I don't know what you meant by \P originally, but now it is used to match Unicode code points, and it requires a parameter (like \P{L}, to match any non-letter character). See: regular-expressions.info/unicode.html
I'm trying to match this: :-P or :P (as in emoticon sticking tongue out)...how would I do that now?
omit the backslash before the P
@knagode I'd recommend you open a new, well-written question for your case. It's very hard to help in the comments section.
|
5

Ruby 2.0 Document

/Pattern/u - stand for UTF-8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.