From: sawadatsuyoshi@... Date: 2019-02-06T14:32:01+00:00 Subject: [ruby-core:91435] [Ruby trunk Feature#15588] String#each_chunk and #chunks Issue #15588 has been updated by sawa (Tsuyoshi Sawada). I am also not so sure if this feature is needed. But if I wanted such feature, I would ask to let `String#scan` take similar arguments as `String#[]`. That is, let the first argument point to the starting position, and an optional second argument to be the length. Since we want to capture multiple matches unlike with `[]`, passing a single index for the first argument does not make much sense, but now we have `Enumerator::ArithmeticSequence`. So we should be able to do ```ruby str.scan((0..).step(9)) #=> ["20190101 ", "20190102\n", "20190103 ", "20190104\n"] str.scan((0..).step(9), 8) #=> ["20190101", "20190102", "20190103", "20190104"] ``` ---------------------------------------- Feature #15588: String#each_chunk and #chunks https://bugs.ruby-lang.org/issues/15588#change-76694 * Author: Glass_saga (Masaki Matsushita) * Status: Open * Priority: Normal * Assignee: * Target version: 2.7 ---------------------------------------- String#each_chunk iterates chunks of specified size in String. String#chunks is a shorthand for str.each_chunk(n).to_a. present: ```ruby str = < "20190101 " end str.scan(/.{1,9}/m) do |chunk| chunk.strip! p chunk #=> "20190101" end str.scan(/.{1,9}/m) #=> ["20190101 ", "20190102\n", "20190103 ", "20190104\n"] str.scan(/.{1,9}/m).map(&:strip) #=> ["20190101", "20190102", "20190103", "20190104"] ``` proposal: ```ruby str = < "20190101 " end str.each_chunk(9, strip: true) do |chunk| p chunk #=> "20190101" end str.chunks(9) #=> ["20190101 ", "20190102\n", "20190103 ", "20190104\n"] str.chunks(9, strip: true) #=> ["20190101", "20190102", "20190103", "20190104"] ``` ---Files-------------------------------- patch.diff (6.56 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: