Skip to content

Commit

Permalink
[Truffle] - Implements String#chr
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasallan committed Dec 22, 2014
1 parent dbff750 commit c1f0e4c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,6 @@ public RubyString capitalize(RubyString string) {

}


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

Expand All @@ -1272,4 +1271,30 @@ public RubyString clear(RubyString string) {
}
}

@CoreMethod(names = "chr")
public abstract static class ChrNode extends CoreMethodNode {

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

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

@Specialization
public RubyString chr(RubyString string) {
notDesignedForCompilation();
if (string.toString().isEmpty()) {
return string;
} else {
String head = string.toString().substring(0, 1);
ByteList byteString = ByteList.create(head);
byteString.setEncoding(string.getBytes().getEncoding());

return string.getContext().makeString(byteString);
}
}
}

}

0 comments on commit c1f0e4c

Please sign in to comment.