From: "scub8040 (Saverio M.)" Date: 2022-07-27T18:42:07+00:00 Subject: [ruby-core:109344] [Ruby master Misc#18942] String splitting handling of empty fields is incorrect or insufficiently documented Issue #18942 has been updated by scub8040 (Saverio M.). austin (Austin Ziegler) wrote in #note-1: > scub8040 (Saverio M.) wrote: > > There are other cases though, which I think are either incorrectly handled, or at least, they should documented. > > This is neither a behaviour bug nor a documentation bug. Uh, ok! Thanks. ---------------------------------------- Misc #18942: String splitting handling of empty fields is incorrect or insufficiently documented https://bugs.ruby-lang.org/issues/18942#change-98486 * Author: scub8040 (Saverio M.) * Status: Open * Priority: Normal ---------------------------------------- Hello! The string splitting needs to deal with some edge cases when it comes to empty strings/fields, for example, an emptry string always returns an empty array. There are other cases though, which I think are either incorrectly handled, or at least, they should documented. The main case is a string exclusively composed of separators, e.g.: ``` "|||".split "|" # => [] ``` Semantically speaking, such splitting does make sense, as an empty field is still a field. As the above example shows though, this returns an empty array (following the explained logic, it should return 4 empty strings). IMO, this is incorrect. If for any reason this isn't, this should be documented though, as it's not obvious behavior (I've referred to this page: https://ruby-doc.org/core-3.0.0/String.html#method-i-split). Things get even more obscure, when there are non-empty fields: ``` "||a|".split "|" # => ["", "", "a"] ``` This result is definitely inconsistent with both logics explained above: - if empty fields should be treated as effective fields, the function should return ["", "", "a", ""] - if empty fields should be ignored, it should return ["a"] Considering this second case, I think that the function is buggy; there's no reason to treat differently the empty fields on the left of a non-empty field, from the ones on the right. Even if this behavior is considered correct, I think it's very valuable to document such cases, as they're not intuitive, especially the second. -- https://bugs.ruby-lang.org/ Unsubscribe: