module PathList::Backports::DeletePrefixSuffix

Public Instance Methods

delete_prefix(str) click to toggle source

delete_prefix(prefix) -> new_str click to toggle source Returns a copy of str with leading prefix deleted.

"hello".delete_prefix("hel") #=> "lo"
"hello".delete_prefix("llo") #=> "hello"
# File lib/path_list/backports.rb, line 46
def delete_prefix(str)
  s = dup
  s.delete_prefix!(str)
  s
end
delete_prefix!(str) click to toggle source

delete_prefix!(prefix) -> self or nil Deletes leading prefix from str, returning nil if no change was made.

"hello".delete_prefix!("hel") #=> "lo"
"hello".delete_prefix!("llo") #=> nil
# File lib/path_list/backports.rb, line 22
def delete_prefix!(str)
  return unless start_with?(str)

  slice!(0..(str.length - 1))
  self
end
delete_suffix(str) click to toggle source

delete_suffix(suffix) -> new_str Returns a copy of str with trailing suffix deleted.

"hello".delete_suffix("llo") #=> "he"
"hello".delete_suffix("hel") #=> "hello"
# File lib/path_list/backports.rb, line 57
def delete_suffix(str) # leftovers:allowed
  s = dup
  s.delete_suffix!(str)
  s
end
delete_suffix!(str) click to toggle source

delete_suffix!(suffix) -> self or nil Deletes trailing suffix from str, returning nil if no change was made.

"hello".delete_suffix!("llo") #=> "he"
"hello".delete_suffix!("hel") #=> nil
# File lib/path_list/backports.rb, line 34
def delete_suffix!(str)
  return unless end_with?(str)

  slice!(-str.length..-1)
  self
end