module Dispel::Tools
Public Class Methods
indexes(string, needle)
click to toggle source
grosser.it/2010/12/31/ruby-string-indexes-indices-find-all-indexes-in-a-string
# File lib/dispel/tools.rb, line 16 def indexes(string, needle) found = [] current_index = -1 while current_index = string.index(needle, current_index+1) found << current_index end found end
naive_split(string, pattern)
click to toggle source
grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/ “ ”.split(' ') == [] “ ”.naive_split(' ') == ['','','',''] “”.split(' ') == [] “”.naive_split(' ') == ['']
# File lib/dispel/tools.rb, line 9 def naive_split(string, pattern) pattern = /#{Regexp.escape(pattern)}/ unless pattern.is_a?(Regexp) result = string.split(pattern, -1) result.empty? ? [''] : result end