[MacRuby-devel] [MacRuby] #529: Using a Proc as a ‘callback function’

MacRuby ruby-noreply at macosforge.org
Mon Mar 22 23:37:31 PDT 2010


#529: Using a Proc as a ‘callback function’
-------------------------------------+--------------------------------------
 Reporter:  eloy.de.enige@…          |        Owner:  martinlagardette@…        
     Type:  defect                   |       Status:  closed                    
 Priority:  blocker                  |    Milestone:  MacRuby 0.6               
Component:  MacRuby                  |   Resolution:  fixed                     
 Keywords:                           |  
-------------------------------------+--------------------------------------
Changes (by martinlagardette@…):

 * cc: eloy.de.enige@… (added)
  * status:  assigned => closed
  * resolution:  => fixed
  * milestone:  => MacRuby 0.6


Old description:

> Some Cocoa methods, or C functions, take pointers to functions which can
> be used as callbacks. For instance, FSEventStreamCreate:
> http://bit.ly/8p70Yw.
>
> RubyCocoa, in conjunction with BridgeSupport, supported this by allowing
> the user to give a proc that would be used as the callback. MacRuby
> should support this too.
>
> Here's a spec example:
>
> {{{
> describe "BridgeSupport" do
>   it "bridges a proc to be used where a pointer to a callback function is
> required" do
>     array = [5, 3, 2, 4, 1]
>     proc = Proc.new do |x, y, context_pointer|
>       context = context_pointer[0].chr + context_pointer[1].chr +
> context_pointer[2].chr
>       x <=> y if context == 'foo'
>     end
>
>     array.sortUsingFunction(proc, context: 'bar')
>     array.should == [5, 3, 2, 4, 1]
>
>     array.sortUsingFunction(proc, context: 'foo')
>     array.should == [1, 2, 3, 4, 5]
>   end
> end
> }}}
>
> Don't know if this is feasible, but it would be great if the arguments
> given to the proc, like the context argument, were no Pointer objects,
> but the object they actually point to:
>
> {{{
> describe "BridgeSupport" do
>   it "bridges a proc to be used where a pointer to a callback function is
> required" do
>     array = [5, 3, 2, 4, 1]
>     proc = Proc.new do |x, y, context|
>       x <=> y if context == 'foo'
>     end
>
>     array.sortUsingFunction(proc, context: 'bar')
>     array.should == [5, 3, 2, 4, 1]
>
>     array.sortUsingFunction(proc, context: 'foo')
>     array.should == [1, 2, 3, 4, 5]
>   end
> end
> }}}

New description:

 Some Cocoa methods, or C functions, take pointers to functions which can
 be used as callbacks. For instance, FSEventStreamCreate:
 http://bit.ly/8p70Yw.

 RubyCocoa, in conjunction with BridgeSupport, supported this by allowing
 the user to give a proc that would be used as the callback. MacRuby should
 support this too.

 Here's a spec example:

 {{{
 #!ruby
 describe "BridgeSupport" do
   it "bridges a proc to be used where a pointer to a callback function is
 required" do
     array = [5, 3, 2, 4, 1]
     proc = Proc.new do |x, y, context_pointer|
       context = context_pointer[0].chr + context_pointer[1].chr +
 context_pointer[2].chr
       x <=> y if context == 'foo'
     end

     array.sortUsingFunction(proc, context: 'bar')
     array.should == [5, 3, 2, 4, 1]

     array.sortUsingFunction(proc, context: 'foo')
     array.should == [1, 2, 3, 4, 5]
   end
 end
 }}}

 Don't know if this is feasible, but it would be great if the arguments
 given to the proc, like the context argument, were no Pointer objects, but
 the object they actually point to:

 {{{
 #!ruby
 describe "BridgeSupport" do
   it "bridges a proc to be used where a pointer to a callback function is
 required" do
     array = [5, 3, 2, 4, 1]
     proc = Proc.new do |x, y, context|
       x <=> y if context == 'foo'
     end

     array.sortUsingFunction(proc, context: 'bar')
     array.should == [5, 3, 2, 4, 1]

     array.sortUsingFunction(proc, context: 'foo')
     array.should == [1, 2, 3, 4, 5]
   end
 end
 }}}

--

Comment:

 Implemented with r3847 :-)

 {{{
 #!ruby
 framework 'Foundation'

 array = [1, 42, 6, 2, 3]
 proc = Proc.new { |a, b, _| a <=> b }
 array.sortedArrayUsingFunction(proc, context: nil) # [1, 2, 3, 6, 42]
 }}}

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/529#comment:5>
MacRuby <http://macruby.org/>



More information about the MacRuby-devel mailing list