Actions
Feature #15588
openString#each_chunk and #chunks
Status:
Open
Assignee:
-
Target version:
-
Description
String#each_chunk iterates chunks of specified size in String.
String#chunks is a shorthand for str.each_chunk(n).to_a.
present:
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:
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
Actions
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0