ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: usa@garbagecollect.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94643] [Ruby master Feature#15588] String#each_chunk and #chunks
Date: Thu, 29 Aug 2019 05:05:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-81241.20190829050538.dc1daa6bdd02b415@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15588.20190206013518@ruby-lang.org

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/

      parent reply	other threads:[~2019-08-29  5:05 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 ` usa [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-81241.20190829050538.dc1daa6bdd02b415@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).