Sha256: b0c21f35c6df2d2b53c6a3afe62bef0d4248d9062caee37ed53975435174b6c7

Contents?: true

Size: 815 Bytes

Versions: 2

Compression:

Stored size: 815 Bytes

Contents

module Dispel
  module Tools
    class << self
      # http://grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/
      # "    ".split(' ') == []
      # "    ".naive_split(' ') == ['','','','']
      # "".split(' ') == []
      # "".naive_split(' ') == ['']
      def naive_split(string, pattern)
        pattern = /#{Regexp.escape(pattern)}/ unless pattern.is_a?(Regexp)
        result = string.split(pattern, -1)
        result.empty? ? [''] : result
      end

      def memoize(*args)
        key = args.map(&:to_s).join("-")
        @memoize ||= {}
        if @memoize.key?(key)
          @memoize[key]
        else
          @memoize[key] = yield
        end
      end

      def last_element(range)
        range.exclude_end? ? range.last.pred : range.last
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dispel-0.0.1 lib/dispel/tools.rb
dispel-0.0.0 lib/dispel/tools.rb