String.tr bug?

Greetings,
I found strange behaviour of string.tr:

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

Is this bug in ruby?

[email protected] wrote:

I found strange behaviour of string.tr:

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

Is this bug in ruby?

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

HTH,
Sebastian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs