From: shevegen@... Date: 2019-01-25T12:06:32+00:00 Subject: [ruby-core:91261] [Ruby trunk Feature#15562] `String#split` option to suppress the initial empty substring Issue #15562 has been updated by shevegen (Robert A. Heiler). This reminds me a bit of Dir['*'] versus Dir.entries(Dir.pwd). The latter also has . and .. entries such as: => ["foobar.md", "..", "."] To me the . and .. entries were never useful. I ended up switching to Dir[] consistently anyway so I don't see . or .., but I am bringing this example because I agree with the statement by sawa about empty strings not being too terribly useful as a result, if you may wish to work with it. Perhaps it may be useful if you wish to .join on it again, but if you are only interested in non-empty results (or non-empty strings) then I think it may be ok to have an additional way to return only the entries you are interested in. Of course you can process the result on your own as-is, via .reject or .select (or .filter), but it may be more convenient to simply pass in another option to .split as second argument. So from this point of view I agree with sawa, even though I personally probably don't need this much at all (oddly enough I think almost all of the use cases I personally have had, were left to pass only one argument to .split()). The only adaptation I would suggest is that I think the proposed syntax is too long. "aba".split("a", terminal_empty_string: :none) # => ["b"] "aba".split("a", terminal_empty_string: :initial) # => ["", "b"] I understand that, I assume, sawa proposes flexibility, which is fine, but it is a bit clumsy and long, IMO. Perhaps something simpler? ignore_empty: true Can't think of many more. Rails/Active* has .blank? which I do not like as a name, but from a conceptual point of view, being able to have a short way to refer to something like the following, may be nice to have in general: "ruby, please ignore nil and empty strings as results, as I need the alternative only". In my own code I (mis)use symbols a lot, so I may propose :ignore_empty_string too. :) (It's actually almost as long as sawa's suggestion, but when I just tried it, making this shorter was not easy, since we lose a bit of meaning what we try to convey here. That is also one reason why it may be useful to somehow refer to situations where we could easily filter away nil and '' empty strings, via a single word/command. Even .blank? may become a bit more verbose if you try to use it via the API above, such as ignore_blanks: true - or something like that. Good API design is hard... ---------------------------------------- Feature #15562: `String#split` option to suppress the initial empty substring https://bugs.ruby-lang.org/issues/15562#change-76509 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- `String#split` returns an empty substring if any at the beginning of the original string, even though it does not return an empty substring at the end of the original string: ```ruby "aba".split("a") # => ["", "b"] ``` This is probably heritage from Perl or AWK, and may have some use cases, but in some (if not most) use cases, this looks asymmetric, and the initial empty string is unnatural and often requires some additional code to remove it. I propose to give an option to `String#split` to suppress it, perhaps like this (with `true` being the default): ```ruby "aba".split("a", initial_empty_string: false) # => ["b"] "aba".split("a", initial_empty_string: true) # => ["", "b"] "aba".split("ba", initial_empty_string: true) # => ["b"] ``` This does not mean to suppress empty strings in the middle. So it should work like this: ```ruby "aaaba".split("a", initial_empty_string: false) # => ["", "", "b"] "aaaba".split("a", initial_empty_string: true) # => ["", "", "", "b"] ``` Or may be we can even go on further to control both the initial and the final ones like (with `:initial` being the default): ```ruby "aba".split("a", terminal_empty_string: :none) # => ["b"] "aba".split("a", terminal_empty_string: :initial) # => ["", "b"] "aba".split("a", terminal_empty_string: :final) # => ["b", ""] "aba".split("a", terminal_empty_string: :both) # => ["", "b", ""] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: