[Kde-bindings] const_missing method in ruby 1.8

Alexander Kellett lypanov at kde.org
Tue Aug 5 19:15:01 UTC 2003


On Tue, Aug 05, 2003 at 10:39:58AM +0100, Richard Dale wrote:
> It probably won't involve a change to do_method_missing in Qt.rb. It might 
> just be a matter of adding this in Qt.cpp, Init_Qt():
> 
>     rb_define_singleton_method(qt_base_class, "const_missing", (VALUE (*) 
> (...)) class_method_missing, -1);
>     rb_define_singleton_method(qt_module, "const_missing", (VALUE (*) (...)) 
> module_method_missing, -1);
>     rb_define_method(qt_base_class, "const_missing", (VALUE (*) (...)) 
> method_missing, -1);
> 
> Just add three new overrides for 'const_missing' which point to the same C 
> functions as the three 'method_missing' equivalents.

oops. not quite perfect. SIGNAL/SLOT are due
to this now handled by const_missing rather 
than the defined methods (as in ruby all cap
identifiers are constants). and are thus 
quite hellishly broken :)

so, here's a patch to hack this in my adding
the ability to return a value to the user in
the rb do_method_missing itself. this improves 
the .rb files flexibility a tad, though it's 
quite a bit of a hack.

possibly you have some better ideas for a fix?
if so, feel free to revert the commit, but as
the bindings are pretty useless at the moment
commiting felt like the right thing to do :)

(or reverting the const_missing change, but
 for some reason i've always detested going
 back as opposed to fixing it :))

Alex
-------------- next part --------------
? signal_fix.patch
Index: Qt.cpp
===================================================================
RCS file: /home/kde/kdebindings/qtruby/rubylib/qtruby/Qt.cpp,v
retrieving revision 1.7
diff -u -p -B -w -r1.7 Qt.cpp
--- Qt.cpp	5 Aug 2003 17:11:53 -0000	1.7
+++ Qt.cpp	5 Aug 2003 19:30:52 -0000
@@ -917,7 +917,9 @@ method_missing(int argc, VALUE * argv, V
 		savestack[count+2] = argv[count];
 	}
 
-	rb_funcall2(qt_internal_module, rb_intern("do_method_missing"), argc+2, savestack);
+	VALUE ret_val = rb_funcall2(qt_internal_module, rb_intern("do_method_missing"), argc+2, savestack);
+	if (ret_val != Qnil)
+		return ret_val;
 
 	// If the method can't be found allow the default method_missing
 	//	to display an error message, by calling super on the method
@@ -960,7 +962,9 @@ class_method_missing(int argc, VALUE * a
 		savestack[count+2] = argv[count];
 	}
 
-	rb_funcall2(qt_internal_module, rb_intern("do_method_missing"), argc+2, savestack);
+	VALUE ret_val = rb_funcall2(qt_internal_module, rb_intern("do_method_missing"), argc+2, savestack);
+	if (ret_val != Qnil)
+		return ret_val;
 
 	// If the method can't be found allow the default method_missing
 	//	to display an error message, by calling super on the method
@@ -1031,7 +1035,9 @@ initialize_qt(int argc, VALUE * argv, VA
 		savestack[count+3] = argv[count];
 	}
 
-	rb_funcall2(qt_internal_module, rb_intern("do_method_missing"), argc+3, savestack);
+	VALUE ret_val = rb_funcall2(qt_internal_module, rb_intern("do_method_missing"), argc+3, savestack);
+	if (ret_val != Qnil)
+		return ret_val;
 
 	// If the method can't be found, allow the default method_missing
 	//	to display an error message, by calling super on the method
Index: lib/Qt/Qt.rb
===================================================================
RCS file: /home/kde/kdebindings/qtruby/rubylib/qtruby/lib/Qt/Qt.rb,v
retrieving revision 1.6
diff -u -p -B -w -r1.6 Qt.rb
--- lib/Qt/Qt.rb	5 Aug 2003 12:24:39 -0000	1.6
+++ lib/Qt/Qt.rb	5 Aug 2003 19:30:52 -0000
@@ -91,9 +91,16 @@ module Qt
 		end
 				
 		def do_method_missing(package, method, klass, *args)
+			if method == "SIGNAL"
+				return "2#{args[0]}"
+			elsif method == "SLOT"
+				return "1#{args[0]}"
+			end
+
 			classname = CppName[klass.name]
 			if classname.nil? and klass != Object
-				return do_method_missing(package, method, klass.superclass, *args)
+				do_method_missing(package, method, klass.superclass, *args)
+				return nil
 			end
 
 			method_original = method.clone
@@ -123,15 +130,10 @@ module Qt
 #						print("Resolved Method #{classname}::#{method} => " + methodIds[0].to_s + "\n")
 						break
 					end
-					puts matching[0][0]
 				end
 			end
-			if !methodIds[0].nil?
-				setCurrentMethod(methodIds[0]) 
-			else
-				fail "\n\t\tcouldn't resolve call to #{klass}.#{method_original}(#{args.join ","}) (resolving to the c++ equivalent of #{classname}::#{method_original})\n" +
-				       "\t\tis it possible that you simply mispelled a local variable access?\n"
-			end
+			setCurrentMethod(methodIds[0] || -1) 
+			return nil
 		end
 		
 		def init()
@@ -267,14 +269,6 @@ module Qt
 		end
 		
 		return meta.metaobject
-	end
-	
-	def SIGNAL(string)
-		return "2" + string
-	end
-	
-	def SLOT(string)
-		return "1" + string
 	end
 	
 	def emit(signal)


More information about the Kde-bindings mailing list