String.tr bug?


#1

Greetings,
I found strange behaviour of string.tr:

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

Is this bug in ruby?


#2

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