3333
3434import org .jruby .anno .JRubyMethod ;
3535import org .jruby .anno .JRubyClass ;
36+ import org .jruby .ast .util .ArgsUtil ;
3637import org .jruby .exceptions .KeyError ;
3738import org .jruby .exceptions .RaiseException ;
39+ import org .jruby .runtime .ThreadContext ;
3840import org .jruby .runtime .builtin .IRubyObject ;
3941
4042/**
4143 * @author Miguel Landaeta
4244 */
4345@ JRubyClass (name ="KeyError" , parent ="IndexError" )
4446public class RubyKeyError extends RubyIndexError {
47+ private static final String [] VALID_KEYS = {"receiver" , "key" };
4548 private IRubyObject receiver ;
4649 private IRubyObject key ;
4750
@@ -67,6 +70,49 @@ protected RaiseException constructThrowable(String message) {
6770 return new KeyError (message , this );
6871 }
6972
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+
70116 @ JRubyMethod
71117 public IRubyObject receiver () {
72118 return receiver ;
0 commit comments