Ric*_*ton 10 ruby ruby-2.0 ruby-2.1
我有两个相似的字符串:
context = "Marriott International World’s Most ADMIRED Lodging Company by FORTUNE for 14th yr. via @FortuneMagazine http://cnnmon.ie/1kcFZSQ"
slice_str = context.slice(105,24) # => "http://cnnmon.ie/1kcFZSQ"
str = "http://cnnmon.ie/1kcFZSQ"
slice_str == str # => true
slice_str.eql? str # => true
Run Code Online (Sandbox Code Playgroud)
但是当我在哈希中查找键是键的字符串时,它们不会在Ruby 2.1.0和Ruby 2.1.1中返回相同的内容:
redirects = {"http://cnnmon.ie/1kcFZSQ"=>""}
redirects.key?(slice_str) # => false
redirects.key?(str) # => true
Run Code Online (Sandbox Code Playgroud)
这种行为有什么解释?Ruby 1.9.3按预期工作.
小智 2
这是 ruby < 2.1.3 中的一个错误
\n\n$ rvm use 2.1.2\nUsing /Users/richniles/.rvm/gems/ruby-2.1.2\n$ irb\n2.1.2 :001 > context = "Marriott International World\xe2\x80\x99s Most ADMIRED Lodging Company by FORTUNE for 14th yr. via @FortuneMagazine http://cnnmon.ie/1kcFZSQ"\n => "Marriott International World\xe2\x80\x99s Most ADMIRED Lodging Company by FORTUNE for 14th yr. via @FortuneMagazine http://cnnmon.ie/1kcFZSQ" \n2.1.2 :002 > slice_str = context.slice(105,24) # => "http://cnnmon.ie/1kcFZSQ"\n => "http://cnnmon.ie/1kcFZSQ" \n2.1.2 :003 > str = "http://cnnmon.ie/1kcFZSQ"\n => "http://cnnmon.ie/1kcFZSQ" \n2.1.2 :004 > redirects = {"http://cnnmon.ie/1kcFZSQ"=>""}\n => {"http://cnnmon.ie/1kcFZSQ"=>""} \n2.1.2 :005 > redirects.key?(slice_str) \n => false \n2.1.2 :006 > redirects.key?(str) \n => true \n
Run Code Online (Sandbox Code Playgroud)\n\n但在 ruby 2.1.3 中做同样的事情:
\n\n $ rvm use 2.1.3\n Using /Users/richniles/.rvm/gems/ruby-2.1.3\n $ irb\n 2.1.3 :001 > context = "Marriott International World\xe2\x80\x99s Most ADMIRED Lodging Company by FORTUNE for 14th yr. via @FortuneMagazine http://cnnmon.ie/1kcFZSQ"\n => "Marriott International World\xe2\x80\x99s Most ADMIRED Lodging Company by FORTUNE for 14th yr. via @FortuneMagazine http://cnnmon.ie/1kcFZSQ" \n 2.1.3 :002 > slice_str = context.slice(105,24) # => "http://cnnmon.ie/1kcFZSQ"\n => "http://cnnmon.ie/1kcFZSQ" \n 2.1.3 :003 > str = "http://cnnmon.ie/1kcFZSQ"\n => "http://cnnmon.ie/1kcFZSQ" \n 2.1.3 :004 > redirects = {"http://cnnmon.ie/1kcFZSQ"=>""}\n => {"http://cnnmon.ie/1kcFZSQ"=>""} \n 2.1.3 :005 > redirects.key?(slice_str) \n => true \n 2.1.3 :006 > redirects.key?(str) \n => true \n
Run Code Online (Sandbox Code Playgroud)\n