Search code examples
objective-crubymotion

How to convert an Objective-c block into Ruby?


I am building a iOS app using Rubymotion. I need to "convert" a piece of Objective-c code into Ruby. Is this correct?

Originally in Objective-c

[movieWriter setCompletionBlock:^{
          [filter removeTarget:movieWriter];
          [movieWriter finishRecording];
      }];

Same thing in Ruby?:

  movieWriter(setCompletionBlock:-> { filter.removeTarget(movieWriter) }, { movieWriter.finishRecording })

Solution

  • No. The syntax for message sends in Ruby is not object(message:arguments) and the syntax for having multiple statements in a block is not {statement1} {statement2}. Instead, you'd want something like this:

    movieWriter.completionBlock = lambda do
      filter.removeTarget(movieWriter)
      movieWriter.finishRecording
    end
    

    (RubyMotion translates setters like completionBlock= into the appropriate setCompletionBlock: method. If you wanted to use the explicit setter method, it would look like movieWriter.setCompletionBlock(lambda do …).)