From: "shugo (Shugo Maeda) via ruby-core" Date: 2025-12-04T03:05:24+00:00 Subject: [ruby-core:124019] [Ruby Feature#21552] allow String.strip and similar to take a parameter similar to String.delete Issue #21552 has been updated by shugo (Shugo Maeda). I just heard someone ask for a strip function that doesn't remove NUL characters. Since Python's `str.strip` takes an optional argument, it might be a good idea to introduce a similar feature. I've created a pull request at and here's a benchmark result: ``` voyager:ruby$ cat benchmark_strip.rb (git)-[feature/allow-strip-to-take[0/1816] require "benchmark" TARGET = " \t\r\n\f\v\0" + "x" * 1024 + "\0 \t\r\n\f\v" Benchmark.bmbm do |x| x.report("strip") do 10000.times do TARGET.strip end end x.report("gsub") do 10000.times do TARGET.gsub(/\A\s+|\s+\z/, "") end end x.report('strip(" \t\r\n\f\v")') do 10000.times do TARGET.strip(" \t\r\n\f\v") end end end voyager:ruby$ ./tool/runruby.rb benchmark_strip.rb (git)-[feature/allow-strip-to-take-chars] Rehearsal -------------------------------------------------------- strip 0.005475 0.000065 0.005540 ( 0.005546) gsub 0.022467 0.000000 0.022467 ( 0.022470) strip(" \t\r\n\f\v") 0.004772 0.000000 0.004772 ( 0.004773) ----------------------------------------------- total: 0.032779sec user system total real strip 0.000759 0.000961 0.001720 ( 0.001720) gsub 0.019911 0.000000 0.019911 ( 0.019912) strip(" \t\r\n\f\v") 0.004958 0.000000 0.004958 ( 0.004961) ``` ---------------------------------------- Feature #21552: allow String.strip and similar to take a parameter similar to String.delete https://bugs.ruby-lang.org/issues/21552#change-115451 * 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/