Skip to content

Commit ca9a132

Browse files
committed
[GR-18163] Update String#split to raise TypeError when false is given
PullRequest: truffleruby/3213
2 parents 4cdbeb7 + 62f0cc4 commit ca9a132

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Compatibility:
3636
* Rewrote `ArrayEachIteratorNode` and re-introduced `each` specs for MRI parity when mutating arrays whilst iterating, rather than crashing (#2587, @MattAlp)
3737
* Update `String#rindex` to only accept `Regexp` or objects convertable to `String` as the first parameter (#2608, @bjfish).
3838
* Update `String#<<` to require one argument (#2609, @bjfish).
39+
* Update `String#split` to raise `TypeError` when false is given (#2606, @bjfish).
3940

4041
Performance:
4142

spec/ruby/core/string/split_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,4 +589,11 @@
589589
end
590590
end
591591
end
592+
593+
it "raises a TypeError when not called with nil, String, or Regexp" do
594+
-> { "hello".split(42) }.should raise_error(TypeError)
595+
-> { "hello".split(:ll) }.should raise_error(TypeError)
596+
-> { "hello".split(false) }.should raise_error(TypeError)
597+
-> { "hello".split(Object.new) }.should raise_error(TypeError)
598+
end
592599
end

src/main/ruby/truffleruby/core/splitter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def split(string, pattern, limit, &orig_block)
6161
return orig_block ? dup_string : result
6262
end
6363

64-
pattern ||= ($; || DEFAULT_PATTERN)
64+
if Primitive.nil?(pattern)
65+
pattern = ($; || DEFAULT_PATTERN)
66+
end
6567

6668
if pattern == DEFAULT_PATTERN
6769
awk_limit = limit < 0 ? -1 : limit

0 commit comments

Comments
 (0)