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
···
--
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
···
-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Wolfgang Nádasi-Donner
Sent: Sunday, August 26, 2007 3:38 AM
To: ruby-talk ML
Subject: 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
--
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
···
-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Wolfgang Nádasi-Donner
Sent: Sunday, August 26, 2007 3:38 AM
To: ruby-talk ML
Subject: 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
--
Posted via http://www.ruby-forum.com/\.
While "ri String#tr" doesn't have anything for me,
But from class String - RDoc Documentation
"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
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
Felix Windt 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
···
--
Posted via http://www.ruby-forum.com/\.