Skip to content

Commit

Permalink
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -1477,8 +1477,8 @@ public IRubyObject initialize(ThreadContext context) {
return initialize19(context);
}

public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
return initialize19(context, args);
public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
return initialize19(context, new IRubyObject[] { arg0 });
}

@JRubyMethod(name = "initialize", visibility = PRIVATE)
@@ -1487,20 +1487,25 @@ public IRubyObject initialize19(ThreadContext context) {
return this;
}

// TODO: Fold kwargs into the @JRubyMethod decorator
final static String[] validInitializeArgs = new String[]{"encoding"};

@JRubyMethod(name = "initialize", visibility = PRIVATE, optional = 2)
public IRubyObject initialize19(ThreadContext context, IRubyObject[] args) {
IRubyObject[] extractedArgs = ArgsUtil.extractKeywordArgs(context, args, validInitializeArgs);
if ( args.length == 0 ) return this;

if ((args.length == 1 && extractedArgs == null) || args.length > 1) {
replace19(args[0]);
}
IRubyObject arg = args[ args.length - 1 ];

if (extractedArgs != null) {
force_encoding(context, extractedArgs[0]);
final IRubyObject string; final IRubyObject encoding;
if ( arg instanceof RubyHash ) { // new encoding: ...
final RubySymbol enc = context.runtime.newSymbol("encoding");
encoding = ( (RubyHash) arg ).fastARef(enc);
string = args.length > 1 ? args[0] : null;
}
else {
string = arg; encoding = null;
}

if ( string != null ) replace19(string);
if ( encoding != null ) force_encoding(context, encoding);

return this;
}

3 comments on commit a3c648d

@cheald
Copy link
Contributor

@cheald cheald commented on a3c648d Dec 13, 2015

Choose a reason for hiding this comment

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

MRI throws an exception if you pass an invalid option there, which is why I used the keyword args. If initially done it this way, but then switched to kwargs after testing behavior versus MRI.

I'll provide sample output when I'm back to my computer later.

Sorry, something went wrong.

@kares
Copy link
Member Author

@kares kares commented on a3c648d Dec 13, 2015

Choose a reason for hiding this comment

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

thanks - in that case it should have been covered with a test-case if you wanted some special behaviour.

Sorry, something went wrong.

@cheald
Copy link
Contributor

@cheald cheald commented on a3c648d Dec 13, 2015

Choose a reason for hiding this comment

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

I'll put together test cases to cover that. Is it desirable for jruby to emulate MRI here, or is the divergence in behavior acceptable for the performance gain?

Please sign in to comment.