Project

General

Profile

Bug #16024

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

Added by tksotn (TAKASHI OOTANI) 4 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
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"]

Associated revisions

Revision f1b76ea6
Added by nobu (Nobuyoshi Nakada) 4 months ago

Occupy match data

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

Revision 1d1f98d4
Added by nobu (Nobuyoshi Nakada) 4 months ago

Reuse match data

  • string.c (rb_str_split_m): reuse occupied match data. [Bug #16024]

Revision 54a470d0
Added by nagachika (Tomoyuki Chikanaga) 3 months ago

merge revision(s) f1b76ea63ce40670071a857f408a4747c571f1e9,1d1f98d49c9908f4e3928e582d31fd2e9f252f92: [Backport #16024]

    Occupy match data

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

    Reuse match data

    * string.c (rb_str_split_m): reuse occupied match data.  [Bug #16024]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 67743
Added by nagachika (Tomoyuki Chikanaga) 3 months ago

merge revision(s) f1b76ea63ce40670071a857f408a4747c571f1e9,1d1f98d49c9908f4e3928e582d31fd2e9f252f92: [Backport #16024]

Occupy match data

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

Reuse match data

* string.c (rb_str_split_m): reuse occupied match data.  [Bug #16024]

History

Updated by qitar888 (Chia-sheng Chen) 4 months 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.

#2

Updated by nobu (Nobuyoshi Nakada) 4 months 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]
#3

Updated by nobu (Nobuyoshi Nakada) 4 months ago

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

Updated by nobu (Nobuyoshi Nakada) 4 months ago

  • Description updated (diff)

Updated by nagachika (Tomoyuki Chikanaga) 3 months 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.

Also available in: Atom PDF