From: Eric Hodel Date: 2011-02-10T09:06:22+09:00 Subject: [ruby-core:35178] Re: [Ruby 1.9-Bug#4386] encoding: directive does not affect regex expressions This regexp has US-ASCII-only characters: > puts /.*/.encoding This regexp is has UTF-8 characters so is in UTF-8 encoding: $ ruby -ve '# coding: UTF-8' -e 'p /��/.encoding' ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0] -e:2: warning: ambiguous first argument; put parentheses or even spaces # Of course, you can't mix: $ ~/.multiruby/install/1.9.2-p136/bin/ruby -ve '# coding: US-ASCII' -e 'p /��/.encoding' ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0] -e:2: warning: ambiguous first argument; put parentheses or even spaces -e:2: invalid multibyte char (US-ASCII) -e:2: invalid multibyte char (US-ASCII) But you can force: $ ~/.multiruby/install/1.9.2-p136/bin/ruby -ve '# coding: UTF-8' -e 'p /.*/u.encoding' ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0] -e:2: warning: ambiguous first argument; put parentheses or even spaces # See also `ri Regexp` in the Encoding section: > A regexp can be matched against a string when they either share an encoding, > or the regexp's encoding is US-ASCII and the string's encoding is > ASCII-compatible. A US-ASCII regexp matches a UTF-8 string correctly: $ ~/.multiruby/install/1.9.2-p136/bin/ruby -ve '# coding: UTF-8' -e 'r = /my ./; p r.encoding, r =~ "my ��", $&' ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0] # 0 "my ��" '.' matches a UTF-8 character even though the regexp is in US-ASCII which has only one-byte characters.