Sha256: 011cf75414185434458939667791e79fd18eaa862a9b05f1fa9406c85828ac57

Contents?: true

Size: 750 Bytes

Versions: 5

Compression:

Stored size: 750 Bytes

Contents

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
    #
    # CREDIT: Lucas Carlson, Blaine Cook

    def start_with?(prefix)
      self.index(prefix) == 0
    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
    #
    # CREDIT: Lucas Carlson, Blaine Cook

    def end_with?(suffix)
      self.rindex(suffix) == size - suffix.size
    end

  end

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

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/string/start_with.rb
facets-2.8.3 lib/core/facets/string/start_with.rb
facets-2.8.2 lib/core/facets/string/start_with.rb
facets-2.8.1 lib/core/facets/string/start_with.rb
facets-2.8.0 lib/core/facets/string/start_with.rb