From: Brian Candler Date: 2009-02-23T22:12:23+09:00 Subject: Re: encoding problem with tr() and hash keys Do One wrote: > Please help to understand solution to this problem (ruby 1.9.1): > > In utf-8 environment I do: > > irb(main):121:0> h = {"a" => 1, "\u0101" => 2} > => {"a"=>1, "ā"=>2} > irb(main):122:0> h.key? "a".tr("z", "\u0101") > => false <--- wrong! > irb(main):123:0> h.key? "\u0101".tr("z", "\u0101") > => true Perhaps describe your environment in more detail? It works for me: $ irb19 irb(main):001:0> h = {"a" => 1, "\u0101" => 2} => {"a"=>1, "ā"=>2} irb(main):002:0> h.key?("a") => true irb(main):003:0> h.key?("\u0101") => true irb(main):004:0> h.key?("a".tr("z", "\u0101")) => true irb(main):005:0> h.key? "a".tr("z", "\u0101") => true irb(main):006:0> h.key? "z".tr("z", "\u0101") => true irb(main):007:0> This is Ubuntu Hardy, ruby 1.9.1 (2008-12-01 revision 20438) [i686-linux] compiled from source. I think this is 1.9.1-preview2 rather than 1.9.1-p0. To eliminate problems with encoding, maybe try writing this as a script and running it from the command line: p h = {"a" => 1, "\u0101" => 2} p h.key?("a") p h.key?("\u0101") p h.key?("a".tr("z", "\u0101")) p h.key? "a".tr("z", "\u0101") p h.key? "z".tr("z", "\u0101") ruby19 test.rb ruby19 -Ku test.rb ruby19 --encoding UTF-8:UTF-8 test.rb to see if this makes any difference. On my machine at least, the -K and --encoding flags are not recognised by irb. -- Posted via http://www.ruby-forum.com/.