Class: String
- Inherits:
-
Object
- Object
- String
- 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
-
#after(delimiter) ⇒ String?
Searches for the first occurrence of
delimiter
, and returns the portion of the String after that. -
#after_last(delimiter) ⇒ String?
Searches for the last occurrence of
delimiter
, and returns the portion of the String after that. -
#before(delimiter) ⇒ String
Searches for the first occurrence of
delimiter
, and returns the portion of the String before that. -
#before_last(delimiter) ⇒ String
Searches for the last occurrence of
delimiter
, and returns the portion of the String before that. -
#between(open, close) ⇒ String?
Returns the portion of the String between the first occurrences of an
open
delimiter and aclose
delimiter. -
#prefix(affix) ⇒ String
Prepends
affix
to the String only if the String does not already start withaffix
. -
#suffix(affix) ⇒ String
Appends
affix
to the String only if the String does not already end withaffix
.
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.
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.
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.
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.
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.
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”).
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”).
13 14 15 |
# File 'lib/casual_support/string/suffix.rb', line 13 def suffix(affix) self.end_with?(affix) ? self.dup : "#{self}#{affix}" end |