Issue #9782 has been updated by Valentin Syrovatskiy.


More accurate description: Array#bsearch cannot find first match while using block with equality condition:

Example:
$ irb
2.0.0-p451 :001 > [1, 2].bsearch{|x| x == 1}
 => nil 
(WRONG, expected 1)
2.0.0-p451 :002 > [1, 2].bsearch{|x| x > 0}
 => 1
(Correct)
2.0.0-p451 :003 > [2, 1].bsearch{|x| x == 1}
 => 1
(Correct)

----------------------------------------
Bug #9782: Array#bsearch skip first match while using block with complex condition
https://bugs.ruby-lang.org/issues/9782#change-46360

* Author: Valentin Syrovatskiy
* Status: Open
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
* ruby -v: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
Description: Array#bsearch cannot find first match when using block with condition by hash key.

Example:
$ irb
2.1.1 :001 > [{a:1, b:2}, {a:3, b:4}].bsearch{|x| x[:a] == 1}
 => nil 
(WRONG, expected {a:1, b:2})
2.1.1 :002 > [{a:3, b:4}, {a:1, b:2}].bsearch{|x| x[:a] == 1}
 => {:a=>1, :b=>2} 
(Correct, successfully find same item if it in second position)
2.1.1 :003 > [{a:3, b:4}, {a:1, b:2}].bsearch{|x| x[:a] == 3}
 => nil 
(WRONG, expected {a:3, b:4})
2.1.1 :004 > [{a:3, b:4}, {a:1, b:2}].bsearch{|x| x[:b] == 4}
 => nil 
(WRONG, expected {a:3, b:4})
2.1.1 :005 > [{a:3, b:4}, {a:1, b:2}, {a:3, b:5}].bsearch{|x| x[:a] == 3}
 => {:a=>3, :b=>5} 
(WRONG, expected {a:3, b:4})

Environment:
$ rvm info
ruby-2.1.1:
  system:
    uname:       "Linux magic 3.14.2-031402-generic #201404262053 SMP Sun Apr 27 00:54:28 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux"
    system:      "ubuntu/12.04/x86_64"
  rvm:
    version:      "rvm 1.25.24 (stable) ..."
  ruby:
    interpreter:  "ruby"
    version:      "2.1.1p76"
    date:         "2014-02-24"
    platform:     "x86_64-linux"
    patchlevel:   "2014-02-24 revision 45161"
    full_version: "ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]"


Bug also can be reproduced with linux ruby 2.0.0 (ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-linux]) and windows ruby 2.0.0 (ruby 2.0.0p451 (2014-02-24) [i386-mingw32]). Other versions and platforms were not tested.



-- 
https://bugs.ruby-lang.org/