Method: String#before_last

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

#before_last(delimiter) ⇒ String

Searches for the last occurrence of delimiter, and returns the portion of the String before that. If delimiter is not found, returns a copy of the original String. Equivalent to split(delimiter, -1)[0...-1].join(delimiter) for existent delimiters.

Examples:

"/path/to/file".before_last("/")  # == "/path/to"
"/path/to/file".before_last(".")  # == "/path/to/file"
"/path/to/file".before_last("")   # == "/path/to/file"

Parameters:

Returns:



16
17
18
# File 'lib/casual_support/string/before_last.rb', line 16

def before_last(delimiter)
  self[0, self.rindex(delimiter) || self.length]
end