bsearch

TIL that we have a builtin bsearch in the Array class. It’s common for Rubyists to default to find, but as we know bsearch performs much better when we have a sorted Array.

Let’s see an example:

ary = [0, 4, 7, 10, 12]
ary.bsearch {|x| x >= 4 } #=> 4
ary.bsearch {|x| x >=  -1 } #=> 0

We can also use it to search String objects, but in this case we need to use the <=> operator in order to return -1, 0 or 1. Here is an explanation of the specifics of this operator.

options = ['Deleted', 'Draft', 'In Review', 'Published', 'Unpublished']
options.bsearch { |option| 'Draft' <=> option }  #=> 'Draft'
options.bsearch { |option| 'Random' <=> option } #=> nil

Notice that the term being compared must be on the left side.