Module: ActiveSupport::CoreExtensions::String::StartsEndsWith

Included in:
String
Defined in:
lib/active_support/core_ext/string/starts_ends_with.rb

Overview

Additional string tests.

Instance Method Summary collapse

Instance Method Details

#ends_with?(suffix) ⇒ Boolean

Does the string end with the specified suffix?

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/active_support/core_ext/string/starts_ends_with.rb', line 13

def ends_with?(suffix)
  suffix = suffix.to_s
  self[-suffix.length, suffix.length] == suffix      
end

#starts_with?(prefix) ⇒ Boolean

Does the string start with the specified prefix?

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/active_support/core_ext/string/starts_ends_with.rb', line 7

def starts_with?(prefix)
  prefix = prefix.to_s
  self[0, prefix.length] == prefix
end