Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/casual_support/string/to.rb,
lib/casual_support/string/drop.rb,
lib/casual_support/string/from.rb,
lib/casual_support/string/last.rb,
lib/casual_support/string/after.rb,
lib/casual_support/string/first.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 a delimiter, and returns the portion of the String after that. If the delimiter is not found, returns nil. Equivalent to split(delimiter, 2) 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 a delimiter, and returns the portion of the String after that. If the delimiter is not found, returns nil. Equivalent to split(delimiter, -1).drop(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 a delimiter, and returns the portion of the String before that. If the delimiter is not found, returns a copy of the original String. Equivalent to split(delimiter, 2) 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 a delimiter, and returns the portion of the String before that. If the delimiter is not found, returns a copy of the original String. Equivalent to split(delimiter, -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 opening and a closing 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

#drop(n) ⇒ String

Drops characters from the beginning of the String, and returns the remainder. If the number of characters to drop is greater than the length of the String, an empty string is returned.

Examples:

"abcdef".drop(0)  # == "abcdef"
"abcdef".drop(3)  # == "def"
"abcdef".drop(6)  # == ""
"abcdef".drop(7)  # == ""

Parameters:

Returns:



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

def drop(n)
  return self.dup if n <= 0
  self[n, self.length] || ''
end

#first(limit = 1) ⇒ String

Returns the first limit characters from the beginning of the String.

This method replaces Active Support’s String#first, except that it returns an empty string when given a negative limit argument, whereas Active Support’s String#first removes limit.abs characters from the end of the String. Returning an empty string makes more sense if you interpret first as “keep upto limit characters.” (At most, a negative limit should keep that many characters from the end of the String, rather than remove that many characters, but returning an empty string is a good conservative choice.) This method is also faster.

Examples:

"abcdef".first(0)   # == ""
"abcdef".first(3)   # == "abc"
"abcdef".first(6)   # == "abcdef"
"abcdef".first(7)   # == "abcdef"
"abcdef".first(-1)  # == ""

Parameters:

Returns:



29
30
31
# File 'lib/casual_support/string/first.rb', line 29

def first(limit = 1)
  self[0, limit] || ''
end

#from(position) ⇒ String

Returns the substring starting at a given position, spanning through the end of the String.

This method replaces Active Support’s String#from. It is faster.

Examples:

"abcdef".from(0)  # == "abcdef"
"abcdef".from(3)  # == "def"
"abcdef".from(6)  # == ""
"abcdef".from(7)  # == ""

Parameters:

Returns:



20
21
22
# File 'lib/casual_support/string/from.rb', line 20

def from(position)
  self[position, self.length]
end

#last(limit = 1) ⇒ String

Returns the last limit characters from the end of the String.

This method replaces Active Support’s String#last, except that it returns an empty string when given a negative limit argument, whereas Active Support’s String#last removes limit.abs characters from the beginning of the String. Returning an empty string makes more sense if you interpret last as “keep upto limit characters.” (At most, a negative limit should keep that many characters from the beginning of the String, rather than remove that many characters, but returning an empty string is a good conservative choice.) This method is also faster.

Examples:

"abcdef".last(0)   # == ""
"abcdef".last(3)   # == "def"
"abcdef".last(6)   # == "abcdef"
"abcdef".last(7)   # == "abcdef"
"abcdef".last(-1)  # == ""

Parameters:

Returns:



28
29
30
# File 'lib/casual_support/string/last.rb', line 28

def last(limit = 1)
  limit < 0 ? '' : (self[-limit, limit] || self.dup)
end

#prefix(affix) ⇒ String

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

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 a suffix to the String only if the String does not already end with that suffix. 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

#to(position) ⇒ String

Returns the substring from the start of the String, spanning through a given position.

This method replaces Active Support’s String#to. It is faster.

Examples:

"abcdef".to(0)  # == "a"
"abcdef".to(2)  # == "abc"
"abcdef".to(5)  # == "abcdef"
"abcdef".to(6)  # == "abcdef"

Parameters:

Returns:



20
21
22
23
# File 'lib/casual_support/string/to.rb', line 20

def to(position)
  position += self.length if position < 0
  self[0, position + 1] || ''
end