Module: Dispel::Tools

Defined in:
lib/dispel/tools.rb

Class Method Summary collapse

Class Method Details

.indexes(string, needle) ⇒ Object



16
17
18
19
20
21
22
23
# 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) ⇒ Object

grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/ “ ”.split(‘ ’) == [] “ ”.naive_split(‘ ’) == [”,”,”,”] “”.split(‘ ’) == [] “”.naive_split(‘ ’) == [”]



9
10
11
12
13
# 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