# A very weird bug in ruby

**URL:** <https://www.ruby-forum.com/t/a-very-weird-bug-in-ruby/187113>\
**Category:** Rails\
**Created:** [April 17, 2010, 11:28am UTC](https://www.ruby-forum.com/t/a-very-weird-bug-in-ruby/187113 "2010-04-17T11:28:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trausti\_Thor\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@Trausti\_Thor\_J](https://www.ruby-forum.com/u/Trausti_Thor_J)\
**Post date:** [April 17, 2010, 11:28am UTC](https://www.ruby-forum.com/t/a-very-weird-bug-in-ruby/187113/1 "2010-04-17T11:28:37Z")

</div>

Yes yes, I think I have found my 2nd compiler bug (not counting  
realbasic). I think I found an actual bug in Ruby, on all platforms  
none the less (well linux and mac).

if yu just run console or irb and do this

ph = “no.305.week”

b = ph+“5”  
returns “no.305.week5”

then do  
b.delete ph

and it returns empty string

If you put anything BUT 5 in the string, everything works. Also if  
you substitute 305 with something that is not a number only everything  
works.

I have put in more thoughts on this in my blog :  
[http://traustithor.blogspot.com/2010/04/most-funny-but-annoying-ruby-bug.html](http://traustithor.blogspot.com/2010/04/most-funny-but-annoying-ruby-bug.html)

If anyone can cast any idea on this as to why this is happening, I  
would love to hear it. I know the bug is in the delete function, I  
want some more and deeper idea as to why.  
This is such an edge case.

Trausti

---

<div class="post-metadata">

**Author:** ![Trausti\_Thor\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@Trausti\_Thor\_J](https://www.ruby-forum.com/u/Trausti_Thor_J)\
**Post date:** [April 17, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/a-very-weird-bug-in-ruby/187113/2 "2010-04-17T12:49:59Z")

</div>

On Apr 17, 10:27 am, Trausti Thor J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> then do  
> b.delete ph
> 
> and it returns empty string
> 
> If you put anything BUT 5 in the string, everything works. Also if  
> you substitute 305 with something that is not a number only everything  
> works.

I think you misunderstand what delete is supposed to do. It does not  
mean ‘find occurrences of the substring and remove them’. Instead it  
removes any characters present in all of the arguments, so for example

“a1b2c3d4e5”.delete(‘12345’) #=\> ‘abcde’  
“a1b2c3d4e5”.delete(‘12345’, ‘123’) #=\> ‘abcd4e5’

In your case the string b, by construction contains only characters  
present in ph (since the 5 that you add at the end of it is already  
present in ph)

Fred

---

<div class="post-metadata">

**Author:** ![Trausti\_Thor\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@Trausti\_Thor\_J](https://www.ruby-forum.com/u/Trausti_Thor_J)\
**Post date:** [April 17, 2010, 12:57pm UTC](https://www.ruby-forum.com/t/a-very-weird-bug-in-ruby/187113/3 "2010-04-17T12:57:01Z")

</div>

Ah ha. That makes delete actually quite cool function. Thank you.

Trausti

On Sat, Apr 17, 2010 at 12:48 PM, Frederick C.
