Sha256: 7bf6fd986f456e8b03c9ea61fce89bc2e8f7628204bfdfb555e17d932fc2ca50

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

#--
# = Developer's Notes
#
# Old definition of start_with? written by Lucas Carlson
# and Blaine Cook was ...
#
#   index(prefix) == 0
# 
# I like the simplicity of this definition, but I could not
# find a way define #end_with? in similar terms and still
# accept regular expressions for the suffix. So I had to use
# Regexp match. On th upside, we can get MatchData
# which might be more useful.
#++

class String

  #unless method_defined?(:start_with?) # 1.8.7+

    # Does a string start with the given prefix?
    #
    #   "hello".start_with?("he")    #=> true
    #   "hello".start_with?("to")    #=> false
    #
    # Note: This definition is better than standard Ruby's
    # becuase it handles regular expressions.
    #
    # CREDIT: Juris Galang

    def start_with?(prefix)
      prefix = Regexp.escape(prefix.to_s) unless Regexp===prefix
      /^#{prefix}/.match(self) ? true : false
    end

  #end

  #unless method_defined?(:end_with?) # 1.8.7+

    # Does a string end with the given suffix?
    #
    #   "hello".end_with?("lo")    #=> true
    #   "hello".end_with?("to")    #=> false
    #
    # Note: This definition is better than standard Ruby's
    # becuase it handles regular expressions.
    #
    # CREDIT: Juris Galang

    def end_with?(suffix)
      suffix = Regexp.escape(suffix.to_s) unless Regexp===suffix
      /#{suffix}$/.match(self) ? true : false
    end

  #end

  alias_method :starts_with?, :start_with?
  alias_method :ends_with?  , :end_with?

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/string/start_with.rb
facets-2.9.2 src/core/facets/string/start_with.rb
facets-2.9.2 lib/core/facets/string/start_with.rb
facets-2.9.1 lib/core/facets/string/start_with.rb
facets-2.9.0 lib/core/facets/string/start_with.rb
facets-2.9.0.pre.2 lib/core/facets/string/start_with.rb
facets-2.9.0.pre.1 lib/core/facets/string/start_with.rb