This blog is part of our Ruby 2.5 series.
Let's say that we have a string Projects::CategoriesController and we want to
remove Controller. We can use
chomp method.
irb> "Projects::CategoriesController".chomp("Controller")
=> "Projects::Categories"
However if we want to remove Projects:: from the string then there is no
corresponding method of chomp. We need to resort to
sub.
irb> "Projects::CategoriesController".sub(/Projects::/, '')
=> "CategoriesController"
Naotoshi Seo did not like using regular expression for such a simple task. He proposed that Ruby should have a method for taking care of such tasks.
Some of the names proposed were remove_prefix, deprefix, lchomp,
remove_prefix and head_chomp.
Matz suggested the name delete_prefix and
this method was born.
irb> "Projects::CategoriesController".delete_prefix("Projects::")
=> "CategoriesController"
Now in order to delete prefix we can use delete_prefix and to delete suffix we
could use chomp. This did not feel right. So for symmetry delete_suffix was
added.
irb> "Projects::CategoriesController".delete_suffix("Controller")
=> "Projects::Categories"
Read up on this discussion to learn more about how elixir, go, python, and PHP deal with similar requirements.

Related blogs
- 01Ruby 2.5 added lazy proc allocation for block parametersMay 22, 2018 · Amit Choudhary
- 02Ruby 2.5 adds Thread.report_on_exception by defaultApril 18, 2018 · Vishal Telangre
- 03Ruby 2.5 adds Exception#full_message methodMarch 13, 2018 · Vishal Telangre
- 04Ruby 2.5 prints backtrace & error message in reverseMarch 7, 2018 · Vishal Telangre