liveBook · Manning

Delete

As explained in The Well-Grounded Rubyist, Fourth Edition

delete is a Ruby String method that returns a string with characters matching specified character-set arguments removed.

String#delete returns a string with characters matching one or more specified character-set arguments removed. Its arguments use the same character-set rules as String#count, including negation and ranges. section 8.1.5

Remove selected characters

"David A. Black".delete("abc") # => "Dvid A. Blk" "David A. Black".delete("^abc") # => "aac" "David A. Black".delete("a-e", "^c") # => "Dvi A. Blck"

Read the argument as a character set rather than as a substring. A leading ^ reverses the selection, a range such as a-e selects a span, and multiple arguments combine character-set conditions. section 8.1.5

Do not confuse the similarly named deletion methods:

  • Set#delete removes one object from a set; deleting an absent object does nothing and does not raise an error. section 9.5.2
  • Dir#delete is an alias for Dir.rmdir and removes a directory rather than characters from a string. section 12.4.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains character-set arguments through literal, negated, ranged, and multiple-argument examples.
Examples
Uses "David A. Black" to demonstrate literal sets, complements, ranges, and multiple arguments.
What it assumes
Assumes familiarity with Ruby strings and introduces character-set selection through concrete examples.
What you can do after
Apply it via Ruby's String API to remove characters selected by literal, negated, ranged, or combined sets.
How it connects
Ties character-set syntax to String#count and contrasts string deletion with Set#delete and Dir#delete.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.