Skip to content

Instantly share code, notes, and snippets.

@acook
Last active December 16, 2015 03:19
Show Gist options
  • Save acook/5368700 to your computer and use it in GitHub Desktop.
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).
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