Skip to content

Commit 16cd846

Browse files
authored
Merge pull request #8858 from PChambino/time-new-with-empty-keywords
Fix `Time.new` when using (empty) keywords (2 issues)
2 parents 3155835 + 5a28062 commit 16cd846

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

core/src/main/java/org/jruby/RubyTime.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,50 +1836,45 @@ private IRubyObject initializeNow(ThreadContext context, IRubyObject zone) {
18361836
@JRubyMethod(name = "initialize", optional = 8, checkArity = false, visibility = PRIVATE, keywords = true)
18371837
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
18381838
boolean keywords = hasKeywords(ThreadContext.resetCallInfo(context));
1839-
int argc = args.length - (keywords ? 1 : 0);
1839+
int argc = args.length;
18401840
IRubyObject zone = context.nil;
18411841
IRubyObject precision = context.nil;
18421842

18431843
if (keywords) {
18441844
IRubyObject[] opts = ArgsUtil.extractKeywordArgs(context, args[args.length - 1], "in", "precision");
1845-
if (opts[0] != null) {
1846-
if (args.length > 7) throw argumentError(context, "timezone argument given as positional and keyword arguments");
1847-
zone = opts[0];
1848-
} else if (args.length > 6) {
1849-
zone = args[6];
1850-
}
18511845

1852-
if (opts[1] != null) {
1853-
if (!(opts[1] instanceof RubyNumeric)) {
1854-
// Weird error since all numerics work at this point so why mention Integer?
1855-
throw typeError(context, str(context.runtime, "no implicit conversion of ", typeAsString(opts[1]), " into Integer"));
1846+
if (opts != null) {
1847+
argc -= 1;
1848+
1849+
if (opts[0] != null) {
1850+
if (argc > 6) {
1851+
throw argumentError(context, "timezone argument given as positional and keyword arguments");
1852+
}
1853+
zone = opts[0];
1854+
}
1855+
1856+
if (opts[1] != null) {
1857+
if (!(opts[1] instanceof RubyNumeric)) {
1858+
// Weird error since all numerics work at this point so why mention Integer?
1859+
throw typeError(context, str(context.runtime, "no implicit conversion of ", typeAsString(opts[1]), " into Integer"));
1860+
}
1861+
precision = opts[1];
18561862
}
1857-
precision = opts[1];
18581863
}
1859-
} else if (args.length > 6) {
1860-
zone = args[6];
18611864
}
18621865

1866+
if (argc > 6) zone = args[6];
1867+
18631868
IRubyObject nil = context.nil;
18641869

18651870
return switch (argc) {
18661871
case 0 -> initializeNow(context, zone);
18671872
case 1 -> timeInitParse(context, args[0], zone, precision);
1868-
case 2 -> keywords ?
1869-
initialize(context, args[0], nil, nil, nil, nil, nil, precision, zone) :
1870-
initialize(context, args[0], args[1], nil, nil, nil, nil, precision);
1871-
case 3 -> keywords ?
1872-
initialize(context, args[0], args[1], nil, nil, nil, nil, precision, zone) :
1873-
initialize(context, args[0], args[1], args[2], nil, nil, nil, precision);
1874-
case 4 -> keywords ?
1875-
initialize(context, args[0], args[1], args[2], nil, nil, nil, precision, zone) :
1876-
initialize(context, args[0], args[1], args[2], args[3], nil, nil, precision);
1877-
case 5 -> keywords ?
1878-
initialize(context, args[0], args[1], args[2], args[3], nil, nil, precision, zone) :
1879-
initialize(context, args[0], args[1], args[2], args[3], args[4], nil, precision);
1880-
case 6 -> keywords ?
1881-
initialize(context, args[0], args[1], args[2], args[3], args[4], nil, precision, zone) :
1882-
initialize(context, args[0], args[1], args[2], args[3], args[4], args[5], precision);
1873+
case 2 -> initialize(context, args[0], args[1], nil, nil, nil, nil, precision, zone);
1874+
case 3 -> initialize(context, args[0], args[1], args[2], nil, nil, nil, precision, zone);
1875+
case 4 -> initialize(context, args[0], args[1], args[2], args[3], nil, nil, precision, zone);
1876+
case 5 -> initialize(context, args[0], args[1], args[2], args[3], args[4], nil, precision, zone);
1877+
case 6 -> initialize(context, args[0], args[1], args[2], args[3], args[4], args[5], precision, zone);
18831878
case 7 -> initialize(context, args[0], args[1], args[2], args[3], args[4], args[5], precision, zone);
18841879
default -> throw argumentError(context, argc, 0, 7);
18851880
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'rspec'
2+
3+
# https://github.com/jruby/jruby/issues/8854
4+
describe 'Time#new with empty keywords' do
5+
it 'should not raise NullPointerException' do
6+
expect(Time.new(2000, 3, 2, **{})).to eq Time.new(2000, 3, 2)
7+
end
8+
end

0 commit comments

Comments
 (0)