Method: String#prefix

Defined in:
lib/casual_support/string/prefix.rb

#prefix(affix) ⇒ String

Prepends affix to the String only if the String does not already start with affix. Otherwise returns a duplicate of the String. Equivalent to gsub(/^(?!affix)/, “affix”).

Examples:

"example.com".prefix("www.")      # == "www.example.com"
"www.example.com".prefix("www.")  # == "www.example.com"

Parameters:

Returns:



13
14
15
# File 'lib/casual_support/string/prefix.rb', line 13

def prefix(affix)
  self.start_with?(affix) ? self.dup : "#{affix}#{self}"
end