From: KitaitiMakoto via ruby-core Date: 2025-12-05T13:48:39+00:00 Subject: [ruby-core:124039] [Ruby Feature#21552] allow String.strip and similar to take a parameter similar to String.delete Issue #21552 has been updated by KitaitiMakoto (��� ������). Thank you, shugo. "someone" he says is me. My use case is here. I want to extract chunks from a file and pass them to a neural network model to detect the file type. The model requires two chunks: the `lstrip`ped beggining portion and the `rstrip`ped ending portion, except that null characters must *not* be stripped. It's useful if I can call: ``` ruby beg_portion.lstrip("\t\n\v\f\r ") # ["\t", "\n", "\v," "\f," "\r", " "] or `/\s/` is preferred? end_portion.rstrip("\t\n\v\f\r ") ``` I'm not sure why the model requires such chunks, but I guess it was trained in Python framework and Python's `strip` family doesn't strip null characters by default. As an aside, I was surprised when I saw null characters were stripped by `lstrip` and `rsrip` because I'm familiar with Regexp's `\s` as "whitespace", though the [String's documentation](https://docs.ruby-lang.org/en/master/String.html#class-String-label-Whitespace+in+Strings) explain what is "whitespace". Tips: For the case of `str = " "*1000+"a"`, `reverse`ing it gets faster than `strip`ping it: ``` ruby str.sub(/\A\s+/, "").reverse.sub(/\A\s+/, "").reverse ``` But if many poeple use the trick just for speed, I don't prefer the situation. ---------------------------------------- Feature #21552: allow String.strip and similar to take a parameter similar to String.delete https://bugs.ruby-lang.org/issues/21552#change-115470 * Author: MSP-Greg (Greg L) * Status: Open ---------------------------------------- Regrading `String.strip` (and `lstrip`, `rstrip`, and `!` versions) Some text data representations differentiate between what one might call vertical and horizontal white space, and the 'strip' methods currently strip both. It would be helpful if they had an optional parameter similar to `String.delete` with a one multi-character selector, so one could do: ```ruby t = str.strip " \t" ``` One can use a regex for this, but this much simpler. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/