Sha256: 4f774ff587a5298fe52ff1d83f381dcbbcc8e15eef1ac4599d43f345f3fc7204

Contents?: true

Size: 868 Bytes

Versions: 26

Compression:

Stored size: 868 Bytes

Contents

class String

  # Breaks a string up into an array based on a regular expression.
  # Similar to scan, but includes the matches.
  #
  #   s = "<p>This<b>is</b>a test.</p>"
  #   s.shatter( /\<.*?\>/ )
  #
  # _produces_
  #
  #   ["<p>", "This", "<b>", "is", "</b>", "a test.", "</p>"]
  #
  def shatter( re )
    r = self.gsub( re ){ |s| "\1" + s + "\1" }
    while r[0,1] == "\1" ; r[0] = '' ; end
    while r[-1,1] == "\1" ; r[-1] = '' ; end
    r.split("\1")
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_shatter
      s = "<p>This<b>is</b>a test.</p>"
      sh = s.shatter( /<.*?>/ )
      e = ["<p>", "This", "<b>", "is", "</b>", "a test.", "</p>"]
      assert_equal(e, sh)
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/shatter.rb
facets-1.0.0 lib/facet/string/shatter.rb
facets-1.0.3 packages/core/lib/facet/string/shatter.rb
facets-1.2.0 lib/facets/core/string/shatter.rb
facets-1.1.0 lib/facet/string/shatter.rb
facets-1.3.0 lib/facets/core/string/shatter.rb
facets-1.2.1 lib/facets/core/string/shatter.rb
facets-1.3.3 lib/facets/core/string/shatter.rb
facets-1.3.1 lib/facets/core/string/shatter.rb
facets-1.3.2 lib/facets/core/string/shatter.rb
facets-1.4.0 lib/facets/core/string/shatter.rb
facets-1.4.1 lib/facets/core/string/shatter.rb
facets-1.4.2 lib/facets/core/string/shatter.rb
facets-1.4.3 lib/facets/core/string/shatter.rb
facets-1.4.5 lib/facets/core/string/shatter.rb
facets-1.4.4 lib/facets/core/string/shatter.rb
facets-1.7.0 lib/facets/core/string/shatter.rb
facets-1.7.30 lib/facets/core/string/shatter.rb
facets-1.7.38 lib/facets/core/string/shatter.rb
facets-1.7.46 lib/facets/core/string/shatter.rb