According to the docs, to use bsearch, your array must be sorted with respect to the block.
Looking at your block results mapped out, [false, false, true, false, false]
is not sorted.
Your block is currently set up to use find-minimum mode, because it returns true
/false
. Instead, try using find-any mode, with a block that returns -1
/0
/1
.
[0, 4, 7, 10, 12].bsearch {|x| 7 <=> x } # => 7
[0, 4, 7, 10, 12].bsearch {|x| 6 <=> x } # => nil
In this case, we use the "comparator" or "spaceship" operator (<=>
), which returns -1
/0
/1
.