[MacRuby-devel] Strange behaviour when using a Proc for a callback

Mark Rada mrada at marketcircle.com
Wed Feb 2 17:51:45 PST 2011


Hi,

I was trying to setup an async callback using a proc instead of a method to register for app launching notifications. For some reason, when I use the proc method, I get the wrong object passed to the proc
when it gets called.

The version using a method callback:

	framework 'Cocoa'

	def callback notif
	  puts notif.class
	  puts notif.inspect
	  puts notif.userInfo.inspect
	end

	def launch_mail
	  workspace = NSWorkspace.sharedWorkspace

	  workspace.notificationCenter.addObserver self,
        	                          selector:'callback:',
                	                      name:NSWorkspaceDidLaunchApplicationNotification,
                        	            object:nil

	  workspace.launchAppWithBundleIdentifier 'com.apple.mail',
        	                          options:NSWorkspaceLaunchAsync,
	           additionalEventParamDescriptor:nil,
        	                 launchIdentifier:nil
	end

	launch_mail
	NSRunLoop.currentRunLoop.runUntilDate( Time.now + 5 )


That code will work the way that I expected it to, but if I try to use a proc:


	framework 'Cocoa'

	callback = Proc.new { |notif|
	  puts notif.class
	  puts notif.inspect
	  puts notif.userInfo.inspect
	}

	workspace = NSWorkspace.sharedWorkspace

	workspace.notificationCenter.addObserver callback,
        	                        selector:'call:',
                	                    name:NSWorkspaceDidLaunchApplicationNotification,
                        	          object:nil

	workspace.launchAppWithBundleIdentifier 'com.apple.mail',
        	                        options:NSWorkspaceLaunchAsync,
	         additionalEventParamDescriptor:nil,
        	               launchIdentifier:nil

	NSRunLoop.currentRunLoop.runUntilDate( Time.now + 5 )


It will crash because for some reason I am not getting the notification back as I should, instead I am getting a reference to the NSDictionary class.

I don't think this is the intended behaviour in MacRuby, but I'm not sure if what I am trying to do is supported. 

Is the ability to use a proc as a callback a supported feature? It has worked for me in other cases.


Mark Rada
mrada at marketcircle.com





More information about the MacRuby-devel mailing list