Project

General

Profile

Actions

Bug #16024

closed

String#split with block. cannot use Regexp in the block.

Added by tksotn (TAKASHI OOTANI) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
[ruby-core:93940]

Description

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
$ cat foo
str = "aaa,bbb;ccc,ddd"
n=0
str.split(/[;,]/) do |word|
    n+=1
    p [n,word.gsub(/\w/,"A")]
end
$ ruby foo
[1, "AAA"]
[2, "\u0000AAA,AAA;AAA,AAA"]
$
$ cat bar
str = "aaa\nb\nccc\nddd\n"
n=0
str.split(/\n/) do |line|
    n+=1
    p [n,line.gsub(/.*/,"A")]
end
$ ruby bar
=> infinite loop

expected output:

[1, "AAA"]
[2, "AAA"]
[3, "AAA"]
[4, "AAA"]

Updated by qitar888 (Chia-sheng Chen) over 5 years ago

tksotn (TAKASHI OOTANI) wrote:

$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
$ cat foo
str = "aaa,bbb;ccc,ddd"
n=0
str.split(/[;,]/) do |word|
n+=1
p [n,word.gsub(/\w/,"A")]
end
$ ruby foo
[1, "AAA"]
[2, "\u0000AAA,AAA;AAA,AAA"]
$

expected output:

[1, "AAA"]
[2, "AAA"]
[3, "AAA"]
[4, "AAA"]

$ cat bar
str = "aaa\nb\nccc\nddd\n"
n=0
str.split(/\n/) do |line|
n+=1
p [n,line.gsub(/.*/,"A")]
end
$ ruby bar
=> infinite loop

Hi,

I tried with str.split().each &block and things work just fine for me.

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|f1b76ea63ce40670071a857f408a4747c571f1e9.


Occupy match data

  • string.c (rb_str_split_m): occupy match data not to be modified
    during yielding the block. [Bug #16024]
Actions #3

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN to 2.5: DONTNEED, 2.6: REQUIRED
Actions #4

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Description updated (diff)

Updated by nagachika (Tomoyuki Chikanaga) over 5 years ago

  • Backport changed from 2.5: DONTNEED, 2.6: REQUIRED to 2.5: DONTNEED, 2.6: DONE

ruby_2_6 r67743 merged revision(s) f1b76ea63ce40670071a857f408a4747c571f1e9,1d1f98d49c9908f4e3928e582d31fd2e9f252f92.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0