0040 String_delete

Delete characters

The String class defines the delete method for deleting characters. Very useful for filtering out unwanted characters. ⚠️ The method doesn’t delete sub-strings, but each character present in argument. delete! modifies the instance and returns nil if there has been no change.

0040-string_delete_1.png

delete doesn’t let you use a regular expression, but supports 2.5 special characters:

  • - to define a range of characters
  • ^ for the negation of a set
  • \ to escape one of these 3 characters

0040-string_delete_2.png

The method definition is delete(*selectors), the splat operator allows multiple arguments. In this case, the intersection of each set determines the characters to be deleted.

0040-string_delete_3.png

Links to Ruby documentation for delete and selection rules.