raineszm avatar raineszm committed 25c13fc

Updated to allow methods to be passed as callbacks.

Comments (0)

Files changed (3)

lib/rubypython/conversion.rb

       Python.PyString_FromString rSymbol.to_s
     end
 
+    def self.rtopProc(rObj)
+      pyMethodDef = Python::PyMethodDef.new
+      callback = Proc.new do |py_self, py_args|
+        rObj.call(*RubyPyProxy.new(py_args).to_a).pObject.pointer
+      end
+      pyMethodDef[:ml_name] = FFI::MemoryPointer.from_string "Proc::#{rObj.object_id}"
+      pyMethodDef[:ml_meth] = callback
+      pyMethodDef[:ml_flags] = Python::METH_VARARGS
+      pyMethodDef[:ml_doc] = nil
+
+      Python::PyCFunction_New pyMethodDef, nil
+    end
+
     #If possible converts a ruby type to an equivalent
     #python native type.
     #@param rObj a native ruby type
         rtopSymbol rObj
       when nil
         rtopNone
-      when Proc
+      when Proc, Method
         rtopProc rObj
       else
         raise UnsupportedConversion.new("Unsupported type for RTOP conversion." )
       rb_hash
     end
 
-    def self.rtopProc(rObj)
-      pyMethodDef = Python::PyMethodDef.new
-      callback = Proc.new do |py_self, py_args|
-        rObj.call(RubyPyProxy.new(py_args).to_a).pObject.pointer
-      end
-      pyMethodDef[:ml_name] = FFI::MemoryPointer.from_string "Proc::#{rObj.object_id}"
-      pyMethodDef[:ml_meth] = callback
-      pyMethodDef[:ml_flags] = Python::METH_VARARGS
-      pyMethodDef[:ml_doc] = nil
-
-      Python::PyCFunction_New pyMethodDef, nil
-    end
-
 
     #Converts a pointer to a Python object into a native ruby type, if
     #possible. Otherwise raises an error.

spec/callback_spec.rb

     RubyPython.start
   end
 
-  it "procs should be accepted as functions" do
-    [
-      [2, 2],
-      ["a", "Word"],
-      [ [1, 2], [3, 4] ]
-    ].each do |args|
-      @objects.apply_callback(AProc, args).should == AProc.call(*args)
+  [
+    [ 'procs', AProc ],
+    [ 'methods', AMethod]
+  ].each do |rb_type, rb_object|
+    it "should accept #{rb_type} as functions" do
+      [
+        [2, 2],
+        ["a", "Word"],
+        [ [1, 2], [3, 4] ]
+      ].each do |args|
+        @objects.apply_callback(rb_object, args).should == rb_object.call(*args)
+      end
     end
   end
 

spec/spec_helper.rb

 
     AProc = Proc.new { |a1, a2| a1 + a2 }
 
+    def self.a_method(a1, a2)
+      a1 + a2
+    end
+
+    AMethod = method(:a_method)
+
 end
 
 def run_python_command(cmd)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.