Skip to content

Commit 1977eb1

Browse files
authored
Support encoding keyword in initialize (JRuby) (#45)
This PR will add support for the `encoding` keyword to `initialize` in the JRuby extension. Unfortunately there appears to be no tests for this behavior, so adding some may have to be part of this work.
1 parent 06ceff7 commit 1977eb1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ext/java/org/jruby/ext/stringio/StringIO.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,19 @@ private void strioInit(ThreadContext context, IRubyObject[] args) {
158158
StringIOData ptr = this.ptr;
159159

160160
synchronized (ptr) {
161-
switch (args.length) {
161+
int argc = args.length;
162+
Encoding encoding = null;
163+
164+
IRubyObject options = ArgsUtil.getOptionsArg(runtime, args);
165+
if (!options.isNil()) {
166+
argc--;
167+
IRubyObject encodingOpt = ArgsUtil.extractKeywordArg(context, "encoding", (RubyHash) options);
168+
if (!encodingOpt.isNil()) {
169+
encoding = EncodingUtils.toEncoding(context, encodingOpt);
170+
}
171+
}
172+
173+
switch (argc) {
162174
case 2:
163175
mode = args[1];
164176
final boolean trunc;
@@ -192,7 +204,7 @@ private void strioInit(ThreadContext context, IRubyObject[] args) {
192204
}
193205

194206
ptr.string = string;
195-
ptr.enc = null;
207+
ptr.enc = encoding;
196208
ptr.pos = 0;
197209
ptr.lineno = 0;
198210
// funky way of shifting readwrite flags into object flags

0 commit comments

Comments
 (0)