Ticket #529 (closed defect: fixed)
Using a Proc as a ‘callback function’
| Reported by: | eloy.de.enige@… | Owned by: | martinlagardette@… |
|---|---|---|---|
| Priority: | blocker | Milestone: | MacRuby 0.6 |
| Component: | MacRuby | Keywords: | |
| Cc: | eloy.de.enige@… |
Description (last modified by martinlagardette@…) (diff)
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
Change History
Note: See
TracTickets for help on using
tickets.

