https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112019-02-06T03:30:47ZRuby Issue Tracking SystemRuby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=766772019-02-06T03:30:47Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Why the <code>String#scan</code> example you showed is not suitable for you? Tell us what makes you happy with the proposal.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=766842019-02-06T12:39:37Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>I like the proposal itself. I don't think that <code>chunks</code> is a good name, though.</p>
<p>To take every n characters, I often write <code>str.scan(/.{1,#{ n }}/m)</code>, but it looks a bit cryptic. In this case <code>str.chunks(n)</code> is simpler.</p>
<p>I dislike <code>strip: true</code>. It is too ad-hoc. Does it also support <code>lstrip: true</code>, <code>rstrip: true</code>, <code>chop: true</code>, <code>chomp: true</code>, etc? In principle, one method should do one thing, IMO.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=766942019-02-06T14:31:59Zsawa (Tsuyoshi Sawada)
<ul></ul><p>I am also not so sure if this feature is needed. But if I wanted such feature, I would ask to let <code>String#scan</code> take similar arguments as <code>String#[]</code>. 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 <code>[]</code>, passing a single index for the first argument does not make much sense, but now we have <code>Enumerator::ArithmeticSequence</code>. So we should be able to do</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">str</span><span class="p">.</span><span class="nf">scan</span><span class="p">((</span><span class="mi">0</span><span class="o">..</span><span class="p">).</span><span class="nf">step</span><span class="p">(</span><span class="mi">9</span><span class="p">))</span> <span class="c1">#=> ["20190101 ", "20190102\n", "20190103 ", "20190104\n"]</span>
<span class="n">str</span><span class="p">.</span><span class="nf">scan</span><span class="p">((</span><span class="mi">0</span><span class="o">..</span><span class="p">).</span><span class="nf">step</span><span class="p">(</span><span class="mi">9</span><span class="p">),</span> <span class="mi">8</span><span class="p">)</span> <span class="c1">#=> ["20190101", "20190102", "20190103", "20190104"]</span>
</code></pre> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=766962019-02-06T15:23:39Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>This requires more concrete real world example.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=786362019-06-17T04:11:10Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul></ul><p>Here is a usecase</p>
<p><a href="https://github.com/socketry/protocol-http2/blob/12875a97e0f82315682191e3bbbaba8b59cb3432/lib/protocol/http2/settings_frame.rb#L236" class="external">https://github.com/socketry/protocol-http2/blob/12875a97e0f82315682191e3bbbaba8b59cb3432/lib/protocol/http2/settings_frame.rb#L236</a></p>
<p>Because I didn't know <code>/....../</code> should be <code>/....../m</code> I wasted at least 2 hours of debugging.</p>
<p>I wish for both <code>each_chunk</code> or <code>each_slice</code> and/or <code>each_unpack</code>.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=786372019-06-17T04:13:38Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul></ul><p>I wonder if we should have consistency with <code>slice</code> and <code>each_slice</code> from <code>Array</code>. But honestly, I don't care, just if it's available.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=786382019-06-17T04:16:05Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul></ul><p>Is size in characters or bytes?</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=801882019-07-29T09:04:46ZGlass_saga (Masaki Matsushita)glass.saga@gmail.com
<ul></ul><blockquote>
<p>I wonder if we should have consistency with slice and <code>each_slice</code> from <code>Array</code>. But honestly, I don't care, just if it's available.</p>
</blockquote>
<p>I like <code>String#each_slice</code> and <code>#slices</code>.</p>
<blockquote>
<p>Is size in characters or bytes?</p>
</blockquote>
<p>Considering consistency with <code>#slice</code> , it is better to have size as characters.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=801892019-07-29T09:22:37ZEregon (Benoit Daloze)
<ul></ul><p>I think <code>String#each_slice(n_chars)</code> would make sense, since it's like <code>str.chars.each_slice(9) { |a| a.join }</code></p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=801912019-07-29T11:01:52Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p><code>#each_slice</code> and <code>#slices</code> seems fine to me as well; I think it is also a better<br>
name than chunks.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=801932019-07-29T11:56:15Zosyo (manga osyo)
<ul></ul><p>I also wanted something like <code># each_slice</code>.<br>
For example, use it when you want to fix the width of the output.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">puts</span> <span class="s2">"abcdefghijklmnopqrstuvwxyz"</span><span class="p">.</span><span class="nf">each_slice</span><span class="p">(</span><span class="mi">5</span><span class="p">).</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="s2">"</span><span class="si">#{</span><span class="n">s</span><span class="si">}</span><span class="s2"><br>"</span> <span class="p">}</span>
<span class="c1"># output:</span>
<span class="c1"># abcde<br></span>
<span class="c1"># fghij<br></span>
<span class="c1"># klmno<br></span>
<span class="c1"># pqrst<br></span>
<span class="c1"># uvwxy<br></span>
<span class="c1"># z<br></span>
</code></pre>
<blockquote>
<blockquote>
<p>Is size in characters or bytes?<br>
Considering consistency with #slice, it is better to have size as characters.</p>
</blockquote>
</blockquote>
<p>I think that there may be multiple <code>String#each_slice_xxx</code> like<code> String#each_xxx</code>.<br>
(e.g. Defined <code>String#each_slice_byte</code> , <code>String#each_slice_char</code> and more...<br>
Also, I think that <code>String#each_slice</code> may be equivalent to<code>String#each_slice_char</code>.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=812392019-08-29T04:43:13Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>As <a class="user active user-mention" href="https://redmine.ruby-lang.org/users/10">@shyouhei (Shyouhei Urabe)</a> mentioned, we'd like to hear the real-world use-case. Extracting fixed-width records may be the purpose. I'm curious about the OP's opinion.</p>
<p>Matz.</p> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=812412019-08-29T05:05:38Zusa (Usaku NAKAMURA)usa@garbagecollect.jp
<ul></ul><p>Just an idea, this method may be useful to treat data of fixed-length record format if it accepts multi column lengths, such as</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">records</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">fixed_length_records_data</span><span class="p">.</span><span class="nf">each_slice</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">zip</span><span class="p">,</span> <span class="n">tel</span><span class="p">,</span> <span class="nb">name</span><span class="o">|</span>
<span class="n">records</span><span class="p">.</span><span class="nf">push</span><span class="p">({</span><span class="ss">zip: </span><span class="n">zip</span><span class="p">,</span> <span class="ss">tel: </span><span class="n">tel</span><span class="p">,</span> <span class="ss">name: </span><span class="nb">name</span><span class="p">})</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #15588: String#each_chunk and #chunkshttps://redmine.ruby-lang.org/issues/15588?journal_id=833882019-12-25T04:28:28Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.7</i></del>)</li></ul>