ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:91414] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
@ 2019-02-06  1:35 ` glass.saga
  2019-02-06  3:30 ` [ruby-core:91416] " shyouhei
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: glass.saga @ 2019-02-06  1:35 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been reported by Glass_saga (Masaki Matsushita).

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:91416] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
  2019-02-06  1:35 ` [ruby-core:91414] [Ruby trunk Feature#15588] String#each_chunk and #chunks glass.saga
@ 2019-02-06  3:30 ` shyouhei
  2019-02-06 12:39 ` [ruby-core:91425] " mame
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: shyouhei @ 2019-02-06  3:30 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by shyouhei (Shyouhei Urabe).


Why the String#scan example you showed is not suitable for you?  Tell us what makes you happy with the proposal.

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-76677

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:91425] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
  2019-02-06  1:35 ` [ruby-core:91414] [Ruby trunk Feature#15588] String#each_chunk and #chunks glass.saga
  2019-02-06  3:30 ` [ruby-core:91416] " shyouhei
@ 2019-02-06 12:39 ` mame
  2019-02-06 14:32 ` [ruby-core:91435] " sawadatsuyoshi
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: mame @ 2019-02-06 12:39 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by mame (Yusuke Endoh).


I like the proposal itself.  I don't think that `chunks` is a good name, though.

To take every n characters, I often write `str.scan(/.{1,#{ n }}/m)`, but it looks a bit cryptic.  In this case `str.chunks(n)` is simpler.

I dislike `strip: true`.  It is too ad-hoc.  Does it also support `lstrip: true`, `rstrip: true`, `chop: true`, `chomp: true`, etc?  In principle, one method should do one thing, IMO.

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-76684

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:91435] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-02-06 12:39 ` [ruby-core:91425] " mame
@ 2019-02-06 14:32 ` sawadatsuyoshi
  2019-02-06 15:23 ` [ruby-core:91437] " naruse
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: sawadatsuyoshi @ 2019-02-06 14:32 UTC (permalink / raw
  To: ruby-core

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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:91437] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-02-06 14:32 ` [ruby-core:91435] " sawadatsuyoshi
@ 2019-02-06 15:23 ` naruse
  2019-06-17  4:11 ` [ruby-core:93192] " samuel
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: naruse @ 2019-02-06 15:23 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by naruse (Yui NARUSE).


This requires more concrete real world example.

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-76696

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93192] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-02-06 15:23 ` [ruby-core:91437] " naruse
@ 2019-06-17  4:11 ` samuel
  2019-06-17  4:13 ` [ruby-core:93193] " samuel
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: samuel @ 2019-06-17  4:11 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by ioquatix (Samuel Williams).


Here is a usecase

https://github.com/socketry/protocol-http2/blob/master/lib/protocol/http2/settings_frame.rb#L233-L240

Because I didn't know `/....../` should be `/....../m` I wasted at least 2 hours of debugging.

I wish for both `each_chunk` or `each_slice` and/or `each_unpack`.

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-78636

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93193] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-06-17  4:11 ` [ruby-core:93192] " samuel
@ 2019-06-17  4:13 ` samuel
  2019-06-17  4:16 ` [ruby-core:93194] " samuel
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: samuel @ 2019-06-17  4:13 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by ioquatix (Samuel Williams).


I wonder if we should have consistency with `slice` and `each_slice` from `Array`. But honestly, I don't care, just if it's available.

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-78637

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93194] [Ruby trunk Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-06-17  4:13 ` [ruby-core:93193] " samuel
@ 2019-06-17  4:16 ` samuel
  2019-07-29  9:04 ` [ruby-core:93993] [Ruby master " glass.saga
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: samuel @ 2019-06-17  4:16 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by ioquatix (Samuel Williams).


Is size in characters or bytes?

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-78638

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93993] [Ruby master Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2019-06-17  4:16 ` [ruby-core:93194] " samuel
@ 2019-07-29  9:04 ` glass.saga
  2019-07-29  9:22 ` [ruby-core:93994] " eregontp
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: glass.saga @ 2019-07-29  9:04 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by Glass_saga (Masaki Matsushita).


> I wonder if we should have consistency with slice and each_slice from Array. But honestly, I don't care, just if it's available.

I like String#each_slice and #slices.

> Is size in characters or bytes?

Considering consistency with #slice, it is better to have size as characters.

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-80188

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93994] [Ruby master Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2019-07-29  9:04 ` [ruby-core:93993] [Ruby master " glass.saga
@ 2019-07-29  9:22 ` eregontp
  2019-07-29 11:01 ` [ruby-core:93996] " shevegen
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: eregontp @ 2019-07-29  9:22 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by Eregon (Benoit Daloze).


I think `String#each_slice(n_chars)` would make sense, since it's like `str.chars.each_slice(9) { |a| a.join }`

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-80189

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93996] [Ruby master Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2019-07-29  9:22 ` [ruby-core:93994] " eregontp
@ 2019-07-29 11:01 ` shevegen
  2019-07-29 11:56 ` [ruby-core:93998] " manga.osyo
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 14+ messages in thread
From: shevegen @ 2019-07-29 11:01 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by shevegen (Robert A. Heiler).


#each_slice and #slices seems fine to me as well; I think it is also a better
name than chunks.


----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-80191

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:93998] [Ruby master Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2019-07-29 11:01 ` [ruby-core:93996] " shevegen
@ 2019-07-29 11:56 ` manga.osyo
  2019-08-29  4:43 ` [ruby-core:94642] " matz
  2019-08-29  5:05 ` [ruby-core:94643] " usa
  13 siblings, 0 replies; 14+ messages in thread
From: manga.osyo @ 2019-07-29 11:56 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by osyo (manga osyo).


I also wanted something like `# each_slice`.
For example, use it when you want to fix the width of the output.

```ruby
puts "abcdefghijklmnopqrstuvwxyz".each_slice(5).map { |s| "#{s}<br>" }
# output:
# abcde<br>
# fghij<br>
# klmno<br>
# pqrst<br>
# uvwxy<br>
# z<br>
```

>> Is size in characters or bytes?
> Considering consistency with #slice, it is better to have size as characters.

I think that there may be multiple `String#each_slice_xxx` like` String#each_xxx`.
(e.g. Defined `String#each_slice_byte` , `String#each_slice_char` and more...
Also, I think that `String#each_slice` may be equivalent to`String#each_slice_char`.


----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-80193

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:94642] [Ruby master Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2019-07-29 11:56 ` [ruby-core:93998] " manga.osyo
@ 2019-08-29  4:43 ` matz
  2019-08-29  5:05 ` [ruby-core:94643] " usa
  13 siblings, 0 replies; 14+ messages in thread
From: matz @ 2019-08-29  4:43 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by matz (Yukihiro Matsumoto).


As @shyouhei 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.

Matz. 

----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-81239

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:94643] [Ruby master Feature#15588] String#each_chunk and #chunks
       [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
                   ` (12 preceding siblings ...)
  2019-08-29  4:43 ` [ruby-core:94642] " matz
@ 2019-08-29  5:05 ` usa
  13 siblings, 0 replies; 14+ messages in thread
From: usa @ 2019-08-29  5:05 UTC (permalink / raw
  To: ruby-core

Issue #15588 has been updated by usa (Usaku NAKAMURA).


Just an idea, this method may be useful to treat data of fixed-length record format if it accepts multi column lengths, such as

```ruby
records = []
fixed_length_records_data.each_slice(7, 10, 20) do |zip, tel, name|
  records.push({zip: zip, tel: tel, name: name})
end
```


----------------------------------------
Feature #15588: String#each_chunk and #chunks
https://bugs.ruby-lang.org/issues/15588#change-81241

* 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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.scan(/.{1,9}/m) do |chunk|
  p chunk #=> "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 = <<EOS
20190101 20190102
20190103 20190104
EOS

str.each_chunk(9) do |chunk|
  p chunk #=> "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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2019-08-29  5:05 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-15588.20190206013518@ruby-lang.org>
2019-02-06  1:35 ` [ruby-core:91414] [Ruby trunk Feature#15588] String#each_chunk and #chunks glass.saga
2019-02-06  3:30 ` [ruby-core:91416] " shyouhei
2019-02-06 12:39 ` [ruby-core:91425] " mame
2019-02-06 14:32 ` [ruby-core:91435] " sawadatsuyoshi
2019-02-06 15:23 ` [ruby-core:91437] " naruse
2019-06-17  4:11 ` [ruby-core:93192] " samuel
2019-06-17  4:13 ` [ruby-core:93193] " samuel
2019-06-17  4:16 ` [ruby-core:93194] " samuel
2019-07-29  9:04 ` [ruby-core:93993] [Ruby master " glass.saga
2019-07-29  9:22 ` [ruby-core:93994] " eregontp
2019-07-29 11:01 ` [ruby-core:93996] " shevegen
2019-07-29 11:56 ` [ruby-core:93998] " manga.osyo
2019-08-29  4:43 ` [ruby-core:94642] " matz
2019-08-29  5:05 ` [ruby-core:94643] " usa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).