Created
March 27, 2024 10:54
-
-
Save havenwood/9a105e9a0077dd81db8497fdd29b7be4 to your computer and use it in GitHub Desktop.
Example Array#indices implementation in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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