Project

General

Profile

Actions

Bug #9782

closed

Array#bsearch skip first match while using block with complex condition

Added by vsyrovat (Valentin Syrovatskiy) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
[ruby-core:62207]

Description

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0