# String#tr - Problem in Code or Documentation?

**URL:** <https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026>\
**Category:** Ruby\
**Created:** [August 26, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026 "2007-08-26T12:37:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [August 26, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026/1 "2007-08-26T12:37:26Z")

</div>

Moin, moin!

The description for “tr” does not state what should happen, if the  
second string ist an empty string. In the actual implementation all  
character from the object string will be deleted, if they appear in the  
first parameter string.

> > > > > Example \>\>\>\>\>  
> > > > > irb(main):001:0\> ‘Hello, world!’.tr(‘aeiou’,’’)  
> > > > > =\> “Hll, wrld!”
> 
> > > > > EoE \>\>\>\>\>

My question is: Is this an Error in the implementation or a special  
case, which is not documented?

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [August 26, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026/2 "2007-08-26T12:49:24Z")

</div>

> second string ist an empty string. In the actual implementation all  
> case, which is not documented?
> 
> ## Wolfgang NÃ¡dasi-Donner
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

While “ri String#tr” doesn’t have anything for me,

But from [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000845:)

“Returns a copy of str with the characters in from\_str replaced by the  
corresponding characters in to\_str. If to\_str is shorter than from\_str,  
it  
is padded with its last character.”

which seems consistent with what you found.

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [August 26, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026/3 "2007-08-26T13:28:28Z")

</div>

> second string ist an empty string. In the actual implementation all  
> case, which is not documented?
> 
> ## Wolfgang NÃ¡dasi-Donner
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

And to finally answer the question: it seems to be a special case that  
is  
not documented. From string.c:

```
if (RSTRING(repl)->len == 0) {
    return rb_str_delete_bang(1, &src, str);
}

```

If an empty string is passed on as the to\_str, simply return the C  
equivalent of String#delete(from\_str).

Hope that _does_ help this time,

Felix

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [August 26, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026/4 "2007-08-26T14:44:42Z")

</div>

Felix W. wrote:

> If an empty string is passed on as the to\_str, simply return the C  
> equivalent of String#delete(from\_str).
> 
> Hope that _does_ help this time,

Fine - thank you very much! It means, that it is done by intention.

WoNÃ¡Do

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [August 26, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/string-tr-problem-in-code-or-documentation/113026/5 "2007-08-26T13:08:26Z")

</div>

> HTH,
> 
> Felix

I completely and utterly missed your point there, being that when the to  
string is empty, there’s no character to pad to the right.

Sorry for the noise,

Felix
