Project

General

Profile

Actions

Bug #10518

closed

String "delete" method does not remove some characters from input string

Added by sergio.gallelli (Sergio Gallelli) about 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.4p265 (2014-10-27 revision 48166) [i386-mingw32]
[ruby-core:66317]

Description

This bug affects all Ruby versions I tried, i.e. 2.0.0 and 2.1.4 on Windows and 2.1.5 on Linux
In some cases the String "delete" method does not remove all the characters from the input string I ask for removal.
The problem seems to depend on the string passed as argument to the delete method and on the order of its characters.
Please take a look at the simple "irb" dump below:

irb(main):001:0> str = 'A-B'
=> "A-B"
irb(main):002:0> str.delete ' -#'
=> "A-B"
irb(main):003:0> str.delete ' #-'
=> "AB"

If I try to remove the dash from string 'A-B' passing ' -#' to the delete method, it does not work.
If I change the order of the characters passed to the delete method, the result is correct.
The attached Ruby program shows the above problem.


Files

str_delete_bug.rb (59 Bytes) str_delete_bug.rb sergio.gallelli (Sergio Gallelli), 11/16/2014 11:15 AM

Updated by Hanmac (Hans Mackowiak) about 10 years ago

need to escape the -
irb(main):001:0> str = 'A-B'
=> "A-B"
irb(main):002:0> str.delete ' \-#'
=> "AB"

because with ' -#' i think you cant to remove from " " to "#"

Updated by normalperson (Eric Wong) about 10 years ago

wrote:

irb(main):001:0> str = 'A-B'
=> "A-B"
irb(main):002:0> str.delete ' -#'
=> "A-B"

' -#' is interpreted as: range of characters from ' ' (space) to '#'.
See String#count documentation

I suppose it is weird and surprising behavior, but I doubt it can change
for compatibility. I would normally use String#tr if I wanted a range.

Updated by shevegen (Robert A. Heiler) about 10 years ago

I looked at the String#count documentation:

http://www.ruby-doc.org/core-2.1.4/String.html#method-i-count

Allow me to copy paste from it:

"Each other_str parameter defines a set of characters to count. The intersection of these sets defines the characters to count in str. Any other_str that starts with a caret ^ is negated. The sequence c1-c2 means all characters between c1 and c2. The backslash character can be used to escape ^ or - and is otherwise ignored unless it appears at the end of a sequence or the end of a other_str."

Firstly, why are the strange code tags there?

Secondly - this is not made very clear to me IMHO that the - character is treated in a special way.

Perhaps the documentation can be made more clear in this regard.

Updated by shevegen (Robert A. Heiler) about 10 years ago

Aha, the code tags do not appear here on bugs.ruby-lang.org, but if you click on the
link above at:

http://www.ruby-doc.org/core-2.1.4/String.html#method-i-count

then you can see them.

Updated by sergio.gallelli (Sergio Gallelli) about 10 years ago

Sorry, I did not realize the dash character stays for range and, as such, must be escaped.
This is probably convenient in some cases, however it might be "dangerous" in other circumstances.
Anyway, this is the way Ruby works, so I think this call can be closed.
Thanks.

Updated by shugo (Shugo Maeda) almost 10 years ago

  • Status changed from Open to Closed

Updated by usa (Usaku NAKAMURA) almost 10 years ago

  • Status changed from Closed to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0