Jonathan Clark
Jonathan Clark

Reputation: 20538

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 })

Upvotes: 1

Views: 466

Answers (1)

Chuck
Chuck

Reputation: 237010

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 …).)

Upvotes: 7

Related Questions