Skip to content

Commit 62f0cc4

Browse files
committed
Update String#split to raise TypeError when false is given
1 parent fc85fe7 commit 62f0cc4

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
@@ -33,6 +33,7 @@ Compatibility:
3333
* Do not call `IO#flush` dynamically from `IO#close` (#2594, @gogainda).
3434
* Implement `rb_str_new_static` for C extensions that use it (@aardvark179).
3535
* Rewrote `ArrayEachIteratorNode` and re-introduced `each` specs for MRI parity when mutating arrays whilst iterating, rather than crashing (#2587, @MattAlp)
36+
* Update `String#split` to raise `TypeError` when false is given (#2606, @bjfish).
3637

3738
Performance:
3839

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)