Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 27, 2024 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/9a105e9a0077dd81db8497fdd29b7be4 to your computer and use it in GitHub Desktop.
Save havenwood/9a105e9a0077dd81db8497fdd29b7be4 to your computer and use it in GitHub Desktop.
Example Array#indices implementation in Ruby
module ArrayIndices
refine Array do
def indices(...)
positions = []
offset = 0
while (position = self[offset..].index(...)&.+(offset))
positions << position
offset = position.succ
end
positions
end
end
end
using ArrayIndices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment