---
title: "Ruby 2.5 added delete_prefix and delete_suffix methods"
description: "delete_prefix and delete_suffix methods are added in Ruby 2.5"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-5-added-delete_prefix-and-delete_suffix-methods"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-5-added-delete_prefix-and-delete_suffix-methods.md"
---

# Ruby 2.5 added delete_prefix and delete_suffix methods

delete_prefix and delete_suffix methods are added in Ruby 2.5

- Author: Amit Choudhary
- Published: November 28, 2017
- Categories: Ruby 2.5, Ruby

#### Ruby 2.4

Let's say that we have a string `Projects::CategoriesController` and we want to
remove `Controller`. We can use
[chomp](https://ruby-doc.org/core-2.4.2/String.html#method-i-chomp) method.

```ruby
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](https://ruby-doc.org/core-2.4.2/String.html#sub-method).

```ruby
irb> "Projects::CategoriesController".sub(/Projects::/, '')
=> "CategoriesController"
```

[Naotoshi Seo](https://bugs.ruby-lang.org/users/6938) 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](https://twitter.com/yukihiro_matz) suggested the name `delete_prefix` and
this method was born.

#### Ruby 2.5.0-preview1

```ruby
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.

```ruby
irb> "Projects::CategoriesController".delete_suffix("Controller")
=> "Projects::Categories"
```

Read up on [this discussion](https://bugs.ruby-lang.org/issues/12694) to learn
more about how elixir, go, python, and PHP deal with similar requirements.

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-5-added-delete_prefix-and-delete_suffix-methods)
