Skip to content

Commit

Permalink
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
Original file line number Diff line number Diff line change
@@ -1249,4 +1249,25 @@ public RubyString capitalize(RubyString string) {

}


@CoreMethod(names = "clear")
public abstract static class ClearNode extends CoreMethodNode {

public ClearNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ClearNode(ClearNode prev) {
super(prev);
}

@Specialization
public RubyString clear(RubyString string) {
notDesignedForCompilation();

string.set(ByteList.create(""));
return string;
}
}

}

1 comment on commit e9444ff

@nirvdrum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this loses the encoding of the string. We need to improve this across the board, but figured I'd mention it here:

$ JRUBY_OPTS="-X+T" bin/jruby -e 's = "abc".force_encoding("UTF-8"); p s.encoding.name; s.clear; p s.encoding.name'
"UTF-8"
"ASCII-8BIT"

The encoding of the cleared string should remain UTF-8.

Please sign in to comment.