Last active
December 16, 2015 03:19
-
-
Save acook/5368700 to your computer and use it in GitHub Desktop.
A method to obtain a list of matching indexes for a substring (or regex).
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
class String | |
def list_of_matching_indexes_for substring | |
array = [] | |
self.scan substring do |letter| | |
array << $~.offset(0).first | |
end | |
array | |
end | |
end | |
def self.my_test | |
text = 'Potato or tomato or pistachio' | |
substring = 'or' | |
text.list_of_matching_indexes_for substring | |
end | |
my_test == [7, 17] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment