
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
The bisect gem provides helpers for dealing with sorted Arrays.
gem install bisect
If you're using Bundler, add gem 'bisect'
to your Gemfile.
There are two functions that you need to know about Bisect.insort
and Bisect.bisect
.
Bisect.insort
adds a new element to the Array, but keeps the Array sorted:
require 'bisect'
a = [1, 2, 4]
Bisect.insort(a, 3)
a == [1, 2, 3, 4]
Bisect.bisect
gives you the index at which the element would have been inserted:
require 'bisect'
a = ['a', 'b', 'd']
Bisect.bisect(a, 'c') == 2
If there are equal elements in the Array then insort
will insert the element after the last equal element. Similarly bisect
will return the index one higher than the last equal element. If you'd like to add new elements before equal elements, use insort_left
and bisect_left
. If you need to be explicit then insort_right
and bisect_right
are aliases for insort
and bisect
.
If you want these methods in your Arrays by default, require 'bisect/core_ext'
If you're using bundler, add gem 'bisect', :require => 'bisect/core_ext'
to your Gemfile.
require 'bisect/core_ext'
a = [1, 2, 4]
a.insort(3)
a == [1, 2, 3, 4]
The problem of maintaining a sorted array keeps cropping up, and I like the Python API. As this kind of code has lots of edge-cases, I'm glad the Pythonistas have debugged it already.
Licensed under the MIT license, bug reports and pull requests welcome.
FAQs
Unknown package
We found that bisect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.