Forum: Ruby string.tr bug?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2789e8c54690b64e746c557f859b43e8?d=identicon&s=25 borasque@borasque.com (Guest)
on 2009-01-18 08:25
(Received via mailing list)
Greetings,
I found strange behaviour of string.tr:

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

Is this bug in ruby?
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-01-18 10:54
(Received via mailing list)
borasque@borasque.com 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
This topic is locked and can not be replied to.