File: active_support/core_ext/string/starts_ends_with.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ActiveSupport#1
  module: CoreExtensions#2
  module: String#3
  module: StartsEndsWith#5
has properties
module method: append_features / 1 #6
method: starts_with? / 1 #22
method: ends_with? / 1 #27

Code

   1  module ActiveSupport #:nodoc:
   2    module CoreExtensions #:nodoc:
   3      module String #:nodoc:
   4        # Additional string tests.
   5        module StartsEndsWith
   6          def self.append_features(base)
   7            if '1.8.7 and up'.respond_to?(:start_with?)
   8              base.class_eval do
   9                alias_method :starts_with?, :start_with?
  10                alias_method :ends_with?, :end_with?
  11              end
  12            else
  13              super
  14              base.class_eval do
  15                alias_method :start_with?, :starts_with?
  16                alias_method :end_with?, :ends_with?
  17              end
  18            end
  19          end
  20 
  21          # Does the string start with the specified +prefix+?
  22          def starts_with?(prefix)
  23            prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix
  24          end
  25 
  26          # Does the string end with the specified +suffix+?
  27          def ends_with?(suffix)
  28            suffix.respond_to?(:to_str) && self[-suffix.length, suffix.length] == suffix
  29          end
  30        end
  31      end
  32    end
  33  end