# String.tr bug?

**URL:** <https://www.ruby-forum.com/t/string-tr-bug/158033>\
**Category:** Ruby\
**Created:** [January 18, 2009, 8:25am UTC](https://www.ruby-forum.com/t/string-tr-bug/158033 "2009-01-18T08:25:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 18, 2009, 8:25am UTC](https://www.ruby-forum.com/t/string-tr-bug/158033/1 "2009-01-18T08:25:34Z")

</div>

Greetings,  
I found strange behaviour of string.tr:

p “=+=/=”.tr("+/=", “,-@”)  
got “.,.-.”  
expected “@,@-@”

Is this bug in ruby?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 18, 2009, 10:54am UTC](https://www.ruby-forum.com/t/string-tr-bug/158033/2 "2009-01-18T10:54:48Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I found strange behaviour of string.tr:
> 
> p “=+=/=”.tr("+/=", “,-@”)  
> got “.,.-.”  
> expected “@,@-@”
> 
> Is this bug in ruby?

It’s not a bug. For tr “,-@” is the same as “,-./0123456789:;\<=\>?@”. You  
have  
to escape the - to use it as a literal “-”:  
“=+=/=”.tr("+/=", ‘,-@’) #=\> “@,@-@”

HTH,  
Sebastian
