# Can't understand for String.delete

**URL:** <https://www.ruby-forum.com/t/cant-understand-for-string-delete/179412>\
**Category:** Ruby\
**Created:** [November 24, 2009, 2:42pm UTC](https://www.ruby-forum.com/t/cant-understand-for-string-delete/179412 "2009-11-24T14:42:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [November 24, 2009, 2:42pm UTC](https://www.ruby-forum.com/t/cant-understand-for-string-delete/179412/1 "2009-11-24T14:42:39Z")

</div>

Returns a copy of _str_ with all characters in the intersection of  
its arguments deleted. Uses the same rules for building the set of  
characters as +String#count+.

```
    "hello".delete "l","lo" #=> "heo"
    "hello".delete "lo" #=> "he"
    "hello".delete "aeiou", "^e" #=> "hell"
    "hello".delete "ej-m" #=> "ho"

```

What does this mean?  
I can’t understand for the rules.

Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [November 24, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/cant-understand-for-string-delete/179412/2 "2009-11-24T16:38:33Z")

</div>

On Tue, Nov 24, 2009 at 2:41 PM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What does this mean?  
> I can’t understand for the rules.

What it means is that it builds a character set performing the  
intersection of all arguments to the method. So if you do  
s.delete(“h”, “he”) the intersection of the arguments is “h”, because  
it’s the only one present in all arguments. If the arguments don’t  
intersect nothing is deleted:

irb(main):001:0\> “hello”.delete “h”, “e”  
=\> “hello”

The other part of the explanation means that as the documentation for  
String#count says  
([class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M000834)):

"Any other\_str that starts with a caret (^) is negated. The sequence  
c1—c2 means all characters between c1 and c2. "

This means that s.delete(“^aeiou”) will remove all consonants and  
leave the vocals, and that s.delete(“a-m”) will delete all characters  
between “a” and “m”:

irb(main):002:0\> “hello”.delete “a-m”  
=\> “o”  
irb(main):003:0\> “hello”.delete “^aeiou”  
=\> “eo”  
irb(main):004:0\>

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [November 25, 2009, 1:25am UTC](https://www.ruby-forum.com/t/cant-understand-for-string-delete/179412/3 "2009-11-25T01:25:20Z")

</div>

ah~ thanks, I got it.  
never saw this syntax in my other languages of programming, 🙂

2009/11/24 JesÃºs Gabriel y GalÃ¡n [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [November 25, 2009, 9:56am UTC](https://www.ruby-forum.com/t/cant-understand-for-string-delete/179412/4 "2009-11-25T09:56:15Z")

</div>

On Wed, Nov 25, 2009 at 1:24 AM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ah~ thanks, I got it.  
> never saw this syntax in my other languages of programming, 🙂

“^” for negating and “-” for character ranges are usual syntax of  
regular expressions.

Jesus.
