Skip to content

Commit 2ef2944

Browse files
Fix for ClassCastException inside Struct#initialize when delegating ... via prepended module
1 parent 51ace55 commit 2ef2944

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ public IRubyObject initialize(ThreadContext context) {
534534
public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
535535
boolean keywords = hasNonemptyKeywords(ThreadContext.resetCallInfo(context));
536536

537-
if (keywords) {
538-
return setupStructValuesFromHash(context, (RubyHash) arg0);
537+
if (keywords && arg0 instanceof RubyHash hash) {
538+
return setupStructValuesFromHash(context, hash);
539539
} else if (RubyStruct.getInternalVariable(context, classOf(), KEYWORD_INIT_VAR).isTrue()) {
540540
if (!(arg0 instanceof RubyHash hash)) throw argumentError(context, 1, 0);
541541
return setupStructValuesFromHash(context, hash);

spec/ruby/core/struct/initialize_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,16 @@
5959
data.version.should == "3.2"
6060
data.platform.should == "OS"
6161
end
62+
63+
it "can be called via delegated ... from a prepended module" do
64+
wrapper = Module.new do
65+
def initialize(...)
66+
super(...)
67+
end
68+
end
69+
70+
klass = Class.new(Struct.new(:a)) { prepend wrapper }
71+
s = klass.new("x")
72+
s.a.should == "x"
73+
end
6274
end

0 commit comments

Comments
 (0)