5-2-6 エンコーディングの扱い

In [2]:
'いろはに'.encoding
Out[2]:
#<Encoding:UTF-8>
In [5]:
str = 'こんにちは'
p str.encoding
#<Encoding:UTF-8>
Out[5]:
#<Encoding:UTF-8>
In [6]:
p new_str = str.encode(Encoding::EUC_JP)
new_str.encoding
"\x{A4B3}\x{A4F3}\x{A4CB}\x{A4C1}\x{A4CF}"
Out[6]:
#<Encoding:EUC-JP>
In [7]:
str = "こんにちは"
p str.encoding
p str.encode!(Encoding::EUC_JP)
str.encoding
#<Encoding:UTF-8>
"\x{A4B3}\x{A4F3}\x{A4CB}\x{A4C1}\x{A4CF}"
Out[7]:
#<Encoding:EUC-JP>
In [8]:
utf8 = 'こんにちは'.encode('UTF-8')
eucjp = 'こんにちは'.encode('EUC-JP')
p utf8 == eucjp
utf8.eql?(eucjp)
false
Out[8]:
false
In [9]:
utf8 + eucjp
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and EUC-JP
(pry):19:in `<main>'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:242:in `catch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:241:in `catch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:241:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/backend.rb:65:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/backend.rb:12:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:87:in `execute_request'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:47:in `dispatch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:37:in `run'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/command.rb:70:in `run_kernel'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/command.rb:34:in `run'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/bin/iruby:5:in `<top (required)>'
/Users/ariga/.rbenv/versions/2.3.0/bin/iruby:23:in `load'
/Users/ariga/.rbenv/versions/2.3.0/bin/iruby:23:in `<main>'
In [10]:
eucjp = 'Hello'.encode('EUC-JP')
utf8 = 'Hello'.encode('UTF-8')
utf16 = 'Hello'.encode('UTF-16') #non ascii compat
ascii = 'Hello'.encode('ASCII-8BIT')

p utf8 == eucjp
p utf8 == utf16
true
false
Out[10]:
false
In [11]:
(utf8 + eucjp).encoding
Out[11]:
#<Encoding:UTF-8>
In [12]:
(eucjp + utf8).encoding
Out[12]:
#<Encoding:EUC-JP>
In [13]:
utf8 + utf16
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and UTF-16
(pry):29:in `<main>'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:242:in `catch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:241:in `catch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib/pry/pry_instance.rb:241:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/backend.rb:65:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/backend.rb:12:in `eval'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:87:in `execute_request'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:47:in `dispatch'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/kernel.rb:37:in `run'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/command.rb:70:in `run_kernel'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/lib/iruby/command.rb:34:in `run'
/Users/ariga/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/iruby-0.2.8/bin/iruby:5:in `<top (required)>'
/Users/ariga/.rbenv/versions/2.3.0/bin/iruby:23:in `load'
/Users/ariga/.rbenv/versions/2.3.0/bin/iruby:23:in `<main>'

次回はついに! 5-3 Regexp から