33
33
34
34
import org .jruby .anno .JRubyMethod ;
35
35
import org .jruby .anno .JRubyClass ;
36
+ import org .jruby .ast .util .ArgsUtil ;
36
37
import org .jruby .exceptions .KeyError ;
37
38
import org .jruby .exceptions .RaiseException ;
39
+ import org .jruby .runtime .ThreadContext ;
38
40
import org .jruby .runtime .builtin .IRubyObject ;
39
41
40
42
/**
41
43
* @author Miguel Landaeta
42
44
*/
43
45
@ JRubyClass (name ="KeyError" , parent ="IndexError" )
44
46
public class RubyKeyError extends RubyIndexError {
47
+ private static final String [] VALID_KEYS = {"receiver" , "key" };
45
48
private IRubyObject receiver ;
46
49
private IRubyObject key ;
47
50
@@ -67,6 +70,49 @@ protected RaiseException constructThrowable(String message) {
67
70
return new KeyError (message , this );
68
71
}
69
72
73
+ @ JRubyMethod
74
+ public IRubyObject initialize (ThreadContext context , IRubyObject messageOrKwargs ) {
75
+ IRubyObject [] receiverKey = ArgsUtil .extractKeywordArgs (context , messageOrKwargs , VALID_KEYS );
76
+
77
+ if (receiverKey == null ) return initialize (context , messageOrKwargs , null );
78
+
79
+ return initializeCommon (context , context .nil , receiverKey );
80
+ }
81
+
82
+ @ JRubyMethod
83
+ public IRubyObject initialize (ThreadContext context , IRubyObject message , IRubyObject kwargs ) {
84
+ IRubyObject [] receiverKey = ArgsUtil .extractKeywordArgs (context , kwargs , VALID_KEYS );
85
+
86
+ return initializeCommon (context , message , receiverKey );
87
+ }
88
+
89
+ private IRubyObject initializeCommon (ThreadContext context , IRubyObject message , IRubyObject [] receiverKey ) {
90
+ IRubyObject receiver ;
91
+ IRubyObject key ;
92
+ if (receiverKey == null ) {
93
+ receiver = context .nil ;
94
+ key = context .nil ;
95
+ } else {
96
+ receiver = receiverKey [0 ];
97
+ key = receiverKey [1 ];
98
+ }
99
+
100
+ setMessage (message );
101
+ this .receiver = receiver ;
102
+ this .key = key ;
103
+
104
+ return context .nil ;
105
+ }
106
+
107
+ @ JRubyMethod
108
+ public IRubyObject initialize (ThreadContext context , IRubyObject message , IRubyObject receiver , IRubyObject key ) {
109
+ setMessage (message );
110
+ this .receiver = receiver ;
111
+ this .key = key ;
112
+
113
+ return context .nil ;
114
+ }
115
+
70
116
@ JRubyMethod
71
117
public IRubyObject receiver () {
72
118
return receiver ;
0 commit comments