Module: Dispel::Tools
- Defined in:
- lib/dispel/tools.rb
Class Method Summary collapse
- .last_element(range) ⇒ Object
- .memoize(*args) ⇒ Object
-
.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(‘ ’) == [”].
Class Method Details
.last_element(range) ⇒ Object
25 26 27 |
# File 'lib/dispel/tools.rb', line 25 def last_element(range) range.exclude_end? ? range.last.pred : range.last end |
.memoize(*args) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/dispel/tools.rb', line 15 def memoize(*args) key = args.map(&:to_s).join("-") @memoize ||= {} if @memoize.key?(key) @memoize[key] else @memoize[key] = yield end 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 |