String#tr - Problem in Code or Documentation?

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

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/.

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

But from 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

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/.

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

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

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

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