Skip to content

Commit d678579

Browse files
committed
* Update String#<< to require one argument
1 parent fc85fe7 commit d678579

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-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#<<` to require one argument (#2609, @bjfish).
3637

3738
Performance:
3839

spec/ruby/core/string/append_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
describe "String#<<" do
66
it_behaves_like :string_concat, :<<
77
it_behaves_like :string_concat_encoding, :<<
8+
9+
it "raises an ArgumentError when given the incorrect number of arguments" do
10+
-> { "hello".send(:<<) }.should raise_error(ArgumentError)
11+
-> { "hello".send(:<<, "one", "two") }.should raise_error(ArgumentError)
12+
end
813
end

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,26 @@ protected RubyString dupAsStringInstance(Object string,
568568

569569
}
570570

571-
@CoreMethod(names = { "<<", "concat" }, optional = 1, rest = true, raiseIfNotMutableSelf = true)
571+
@CoreMethod(names = "<<", required = 1, raiseIfNotMutableSelf = true)
572+
@ImportStatic(StringGuards.class)
573+
public abstract static class StringConcatOneNode extends CoreMethodArrayArgumentsNode {
574+
575+
@Specialization(guards = "libFirst.isRubyString(first)")
576+
protected RubyString concat(RubyString string, Object first,
577+
@Cached StringAppendPrimitiveNode stringAppendNode,
578+
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary libFirst) {
579+
return stringAppendNode.executeStringAppend(string, first);
580+
}
581+
582+
@Specialization(guards = "isNotRubyString(first)")
583+
protected Object concatGeneric(RubyString string, Object first,
584+
@Cached DispatchNode callNode) {
585+
return callNode.call(coreLibrary().truffleStringOperationsModule, "concat_internal", string, first);
586+
}
587+
588+
}
589+
590+
@CoreMethod(names = "concat", optional = 1, rest = true, raiseIfNotMutableSelf = true)
572591
@ImportStatic(StringGuards.class)
573592
public abstract static class StringConcatNode extends CoreMethodArrayArgumentsNode {
574593

0 commit comments

Comments
 (0)