We write about Ruby on Rails, React.js, React Native, remote work, open source, engineering and design.
Let's say that we have a string
want to remove
We can use chomp method.
1irb> "Projects::CategoriesController".chomp("Controller") 2=> "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
1irb> "Projects::CategoriesController".sub(/Projects::/, '') 2=> "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
Matz suggested the name
and this method was born.
1irb> "Projects::CategoriesController".delete_prefix("Projects::") 2=> "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.
1irb> "Projects::CategoriesController".delete_suffix("Controller") 2=> "Projects::Categories"
Read up on this discussion to learn more about how elixir, go, python, and PHP deal with similar requirements.