From 1977eb10e34b6a0c8a733f73925061bff02d7ead Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Thu, 13 Apr 2023 15:45:53 -0400 Subject: [PATCH] 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. --- ext/java/org/jruby/ext/stringio/StringIO.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ext/java/org/jruby/ext/stringio/StringIO.java b/ext/java/org/jruby/ext/stringio/StringIO.java index 638327e..bb87bfd 100644 --- a/ext/java/org/jruby/ext/stringio/StringIO.java +++ b/ext/java/org/jruby/ext/stringio/StringIO.java @@ -158,7 +158,19 @@ private void strioInit(ThreadContext context, IRubyObject[] args) { StringIOData ptr = this.ptr; synchronized (ptr) { - switch (args.length) { + int argc = args.length; + Encoding encoding = null; + + IRubyObject options = ArgsUtil.getOptionsArg(runtime, args); + if (!options.isNil()) { + argc--; + IRubyObject encodingOpt = ArgsUtil.extractKeywordArg(context, "encoding", (RubyHash) options); + if (!encodingOpt.isNil()) { + encoding = EncodingUtils.toEncoding(context, encodingOpt); + } + } + + switch (argc) { case 2: mode = args[1]; final boolean trunc; @@ -192,7 +204,7 @@ private void strioInit(ThreadContext context, IRubyObject[] args) { } ptr.string = string; - ptr.enc = null; + ptr.enc = encoding; ptr.pos = 0; ptr.lineno = 0; // funky way of shifting readwrite flags into object flags