Class: String
- Inherits:
-
Object
- Object
- String
- 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
-
#after(delimiter) ⇒ String?
Searches for the first occurrence of a delimiter, and returns the portion of the String after that.
-
#after_last(delimiter) ⇒ String?
Searches for the last occurrence of a delimiter, and returns the portion of the String after that.
-
#before(delimiter) ⇒ String
Searches for the first occurrence of a delimiter, and returns the portion of the String before that.
-
#before_last(delimiter) ⇒ String
Searches for the last occurrence of a 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 opening and a closing delimiter.
-
#drop(n) ⇒ String
Drops characters from the beginning of the String, and returns the remainder.
-
#first(limit = 1) ⇒ String
Returns the first
limit
characters from the beginning of the String. -
#from(position) ⇒ String
Returns the substring starting at a given position, spanning through the end of the String.
-
#last(limit = 1) ⇒ String
Returns the last
limit
characters from the end of the String. -
#prefix(affix) ⇒ String
Prepends a prefix to the String only if the String does not already start with that prefix.
-
#suffix(affix) ⇒ String
Appends a suffix to the String only if the String does not already end with that suffix.
-
#to(position) ⇒ String
Returns the substring from the start of the String, spanning through a given position.
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).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 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)[-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 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)[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 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)[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 opening and a closing 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 |
#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.
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. However, it returns an empty string when given a negative limit
, whereas Active Support 6.0 and previous remove limit.abs
characters from the end of the String. Returning an empty string is more intuitive behavior if first
is interpreted as “keep up to limit
characters.” (At most, a negative limit
should keep limit.abs
characters from the end of the String, instead of remove that many characters, but returning an empty string is a conservative compromise.) This method is also faster than Active Support’s implementation.
30 31 32 |
# File 'lib/casual_support/string/first.rb', line 30 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.
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. However, it returns an empty string when given a negative limit
, whereas Active Support 6.0 and previous remove limit.abs
characters from the beginning of the String. Returning an empty string is more intuitive behavior if last
is interpreted as “keep up to limit
characters.” (At most, a negative limit
should keep limit.abs
characters from the beginning of the String, instead of remove that many characters, but returning an empty string is a conservative compromise.) This method is also faster than Active Support’s implementation.
29 30 31 |
# File 'lib/casual_support/string/last.rb', line 29 def last(limit = 1) self[[length - limit, 0].max, limit] || "" 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”).
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”).
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.
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 |