[MacRuby-devel] C-level blocks (issue #712)?

Matt Aimonetti mattaimonetti at gmail.com
Sun Jan 9 14:59:22 PST 2011


You are right, still a gotcha tho:

Ruby 1.9.2:

  def foo(a, b={})
    b[:handler].call if b.has_key?(:handler)
  end
  foo :bar, handler: Proc.new do
    puts 42
  end

`new': tried to create Proc object without a block (ArgumentError)

def foo(a, b={})
    b[:handler].call if b.has_key?(:handler)
  end
  foo :bar, handler: Proc.new{
    puts 42
  }

Works fine.

I guess this is due to the fact that we are using CRuby's parser.
I got a bit confused because MacRuby allows you to define methods like that:

def foo(a, b:handler)
  handler.call
end

foo :bar, b: Proc.new{ puts 42 }

Which is an extension of the Ruby syntax.
Anyways, thanks for the heads up, I didn't notice it got fixed in 0.8

- Matt

On Sun, Jan 9, 2011 at 2:44 PM, Laurent Sansonetti <lsansonetti at apple.com>wrote:

> This seems to be a Ruby syntax point, unrelated to MacRuby.
>
> Laurent
>
> On Jan 9, 2011, at 2:32 PM, Matt Aimonetti wrote:
>
> Laurent you are right, however I noticed a little gotcha:
>
> If you use the following code:
>
>     panel.beginSheetModalForWindow window, completionHandler: Proc.new do
> |result|
>       return if (result == NSCancelButton)
>       path = panel.filename
>       dest_path = applicationSupportFolder
>       guid = NSProcessInfo.processInfo.globallyUniqueString
>       dest_path = "#{dest_path}/#{guid}"
>       error = Pointer.new(:id)
>       NSFileManager.defaultManager.copyItemAtPath(path, toPath:dest_path,
> error:error)
>       NSApp.presentError(error) if error[0]
>       movie.setValue(dest_path, forKey:"imagePath")
>     end
>
> You get an argument error: tried to create Proc object without a block
> (ArgumentError)
>
> However using the curly brackets would work:
>
> panel.beginSheetModalForWindow window, completionHandler: Proc.new{|result|
>       return if (result == NSCancelButton)
>       path = panel.filename
>       dest_path = applicationSupportFolder
>       guid = NSProcessInfo.processInfo.globallyUniqueString
>       dest_path = "#{dest_path}/#{guid}"
>       error = Pointer.new(:id)
>       NSFileManager.defaultManager.copyItemAtPath(path, toPath:dest_path,
> error:error)
>       NSApp.presentError(error) if error[0]
>       movie.setValue(dest_path, forKey:"imagePath")
>  }
>
> Finally, the only way to get the do/end proc approach to work is to use the
> parenthesis around the arguments:
>
>    panel.beginSheetModalForWindow(window, completionHandler: Proc.new do
> |result|
>       return if (result == NSCancelButton)
>       path = panel.filename
>       dest_path = applicationSupportFolder
>       guid = NSProcessInfo.processInfo.globallyUniqueString
>       dest_path = "#{dest_path}/#{guid}"
>       error = Pointer.new(:id)
>       NSFileManager.defaultManager.copyItemAtPath(path, toPath:dest_path,
> error:error)
>       NSApp.presentError(error) if error[0]
>       movie.setValue(dest_path, forKey:"imagePath")
>    end)
>
> - Matt
>
> On Sun, Jan 9, 2011 at 2:22 PM, Laurent Sansonetti <lsansonetti at apple.com>wrote:
>
>> Hi Rolando,
>>
>> The syntax is simple, you simply pass a Proc project.
>>
>> Here is an example:
>>
>> framework 'Foundation'
>> a = [1, 2, 3, 4, 5]
>> a.enumerateObjectsUsingBlock(Proc.new { |obj, index, stop|
>>  p obj
>> stop.assign(true) if index == 2
>> })
>>
>> It looks like the snippet you pasted should just work. Did you find a
>> problem?
>>
>> Laurent
>>
>>
>> On Jan 9, 2011, at 8:09 AM, Rolando Abarca wrote:
>>
>> Hi all,
>>
>> I've taken a look at the referred issue (#712), but I'm not sure of
>> the final implemented syntax. I'm trying to use a NSOpenPanel, so far
>> this is what I'm doing:
>>
>>
>> {{{
>>  def loadSprite(sender)
>>    @panel.beginSheetModalForWindow(@window, completionHandler:lambda {
>> |code|
>>      NSLog("code: #{code}")
>>    })
>>  end
>> }}}
>>
>> what's the Right Way(tm) to use a NSOpenPanel?
>> thanks!
>> --
>> Rolando Abarca M.
>> Games For Food S.p.A.
>> http://www.gamesforfood.com
>> Phone: +1 (408) 345-5433
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>>
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20110109/c3306e15/attachment-0001.html>


More information about the MacRuby-devel mailing list