On further digging, it seems like the exception raised when you return from a block (/Proc/lambda) is MacRuby specific. Seems like this should have been fixed a while ago:
http://www.macruby.org/trac/ticket/412

I wonder if this bug should be re-opened? I actually encountered a similar problem recently, and it was entirely within the scope of Ruby code I had written, so it doesn't seem like this could be an issue with how the Cocoa code is calling the Proc in Martin's example.

I'm hoping Laurent or someone can offer some ideas on this, because I'm still a little confused as to how exactly this exception. For instance, consider this code that's hooked up to a button's action:

def trigger_action(sender)

a_proc = Proc.new { |var|

puts "hello in proc"

return 1

}

a_proc.call('test')

puts "hello after proc"

end


That code runs without triggering an exception, and prints only "hello in proc" as expected. However, this variation on the code behaves differently:


def trigger_action(sender)

a_proc = Proc.new { |var|

puts "hello in proc"

return 1

}

a_lam = lambda { |var|

puts "hello in lambda"

return 1

}

a_lam.call('test')

puts "hello after lambda"

a_proc.call('test')

puts "hello after proc"

end


Now the code outputs the following:


hello in lambda

hello after lambda

hello in proc

uncaught Objective-C/C++ exception...

terminate called after throwing an instance of 'RoxorReturnFromBlockException*'

Program received signal:  “SIGABRT”.

sharedlibrary apply-load-rules all


I can't quite understand why.


Anybody?


-Gabriel



On Wed, Mar 30, 2011 at 11:05 AM, Rob Gleeson <rob@flowof.info> wrote:

On 30 Mar 2011, at 18:23, Gabriel Gilder wrote:

> I think you want "break" instead of "return". You can't return from a block (Proc).
>
> -Gabriel

That's not strictly true, you can if the Proc.new { } object was initialized within a method (like it is here).
However calling return explicitly will not return control to the caller.

Perhaps you could try a lambda, which behaves like a method when you use return explicitly (returns control to the caller), or
use break as Gabriel suggested.

- Rob
_______________________________________________
MacRuby-devel mailing list
MacRuby-devel@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel