Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/casual_support/string/after.rb,
lib/casual_support/string/before.rb,
lib/casual_support/string/prefix.rb,
lib/casual_support/string/suffix.rb,
lib/casual_support/string/between.rb,
lib/casual_support/string/after_last.rb,
lib/casual_support/string/before_last.rb

Instance Method Summary collapse

Instance Method Details

#after(delimiter) ⇒ String?

Searches for the first occurrence of delimiter, and returns the portion of the String after that. If delimiter is not found, returns nil. Equivalent to split(delimiter, 2).drop(1)[-1] for non-empty delimiters.

Examples:

"http://www.example.com".after("://")  # == "www.example.com"
"http://www.example.com".after("?")    # == nil
"http://www.example.com".after("")     # == "http://www.example.com"

Parameters:

Returns:



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

def after(delimiter)
  i = self.index(delimiter)
  i && self[i + delimiter.length, self.length]
end

#after_last(delimiter) ⇒ String?

Searches for the last occurrence of delimiter, and returns the portion of the String after that. If delimiter is not found, returns nil. Equivalent to split(delimiter, -1).drop(1)[-1] for non-empty delimiters.

Examples:

"/path/to/file".after_last("/")  # == "file"
"/path/to/file".after_last(".")  # == nil
"/path/to/file".after_last("")   # == ""

Parameters:

Returns:



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

def after_last(delimiter)
  i = self.rindex(delimiter)
  i && self[i + delimiter.length, self.length]
end

#before(delimiter) ⇒ String

Searches for the first 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, 2)[0] for non-empty delimiters.

Examples:

"http://www.example.com".before("://")  # == "http"
"http://www.example.com".before("?")    # == "http://www.example.com"
"http://www.example.com".before("")     # == ""

Parameters:

Returns:



15
16
17
# File 'lib/casual_support/string/before.rb', line 15

def before(delimiter)
  self[0, self.index(delimiter) || self.length]
end

#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

#between(open, close) ⇒ String?

Returns the portion of the String between the first occurrences of an open delimiter and a close delimiter. If either delimiter is not found, returns nil.

Examples:

"i <b><3</b> ruby".between("<b>", "</b>")  # == "<3"
"i <b><3<b> ruby".between("<b>", "</b>")   # == nil

Parameters:

Returns:



14
15
16
17
18
19
20
21
# File 'lib/casual_support/string/between.rb', line 14

def between(open, close)
  i = self.index(open)
  if i
    i += open.length
    j = self.index(close, i)
    self[i, j - i] if j
  end
end

#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

#suffix(affix) ⇒ String

Appends affix to the String only if the String does not already end with affix. Otherwise returns a duplicate of the String. Equivalent to gsub(/(?<!affix)$/, “affix”).

Examples:

"example".suffix(".com")      # == "example.com"
"example.com".suffix(".com")  # == "example.com"

Parameters:

Returns:



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

def suffix(affix)
  self.end_with?(affix) ? self.dup : "#{self}#{affix}"
end