Sha256: 7fc6d0476b7d1e2fb0ee7a6530d759c1a6a8b7e7dfaa15504cbd470a978bbfc0

Contents?: true

Size: 812 Bytes

Versions: 25

Compression:

Stored size: 812 Bytes

Contents

class String

  # Turns a string into a regular expression.
  # By default it will escape all characters.
  # Use <tt>false</tt> argument to turn off escaping.
  #
  #   "[".to_re  #=> /\[/
  #
  def to_re(esc=true)
    Regexp.new((esc ? Regexp.escape(self) : self))
  end

  # Turns a string into a regular expression.
  # Unlike #to_re this will not escape characters.
  #
  #   "a?".to_rx  #=> /a?/
  #
  def to_rx
    Regexp.new(self)
  end

end


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

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_to_re
      assert_equal( /abc/, "abc".to_re )
      assert_equal( /a\+bc/, "a+bc".to_re )
      assert_equal( /a+bc/, "a+bc".to_re(false) )
    end

  end

=end

Version data entries

25 entries across 25 versions & 1 rubygems

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