Project

General

Profile

Actions

Bug #13925

closed

string.split(pattern, 1) should return [self.dup], but it returns [self]

Added by tompng (tomoya ishida) about 7 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
[ruby-core:82899]

Description

msg = 'abababababa'
msg.split('b', 4).map(&:upcase!) # won't overwrite msg
puts msg # abababababa (expected)
msg.split('b', 1).map(&:upcase!) # this overwrites msg
puts msg # ABABABABABA (abababababa is expected)

I wrote a patch for this.


Files

split_limit_1.patch (352 Bytes) split_limit_1.patch tompng (tomoya ishida), 09/20/2017 04:31 PM
partition_rpartition.patch (842 Bytes) partition_rpartition.patch hanachin (Seiei Miyagi), 09/20/2017 11:44 PM

Updated by hanachin (Seiei Miyagi) about 7 years ago

There are same problem in partition and rpartition

I expect the s is "abc", but it is modified.

s = "abc"; s.partition("d").map(&:upcase!); s
# => "ABC"

I expect the s is "abc", but it is modified.

s = "abc"; s.rpartition("d").map(&:upcase!); s
# => "ABC"

If string is splitted, partition/rpartition returns the copy of a part of string.

s = "abc"; s.partition("b").map(&:upcase!); s
# => "abc"

s = "abc"; s.rpartition("b").map(&:upcase!); s
# => "abc"

Updated by hanachin (Seiei Miyagi) about 7 years ago

I added some tests and send a Pull Request to ruby/ruby
https://github.com/ruby/ruby/pull/1705

Actions #3

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

  • Status changed from Open to Closed
  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: REQUIRED

Updated by nagachika (Tomoyuki Chikanaga) over 6 years ago

  • Backport changed from 2.2: REQUIRED, 2.3: REQUIRED, 2.4: REQUIRED to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE

ruby_2_4 r62667 merged revision(s) 60000,60001,60002.

Updated by usa (Usaku NAKAMURA) over 6 years ago

  • Backport changed from 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE to 2.2: REQUIRED, 2.3: DONE, 2.4: DONE

ruby_2_3 r62818 merged revision(s) 60000,60001,60002.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0