From: Satoshi N. <sna...@in...> - 2007-12-05 00:33:36
|
Hi, For example, you need to make a callable method from ObjC in a ruby class: def evalScript_source(script, source) instance_eval(script.to_s, source.to_s) end Then you can call the method as bellow: [rubyObj evalScript:@"p (0...10).to_a" source:@"file"]; If you want to call three parameters version, define a ruby method like: def evalScript_source_lineNumber(script, source, line) instance_eval(script.to_s, source.to_s, line) end objc_method :evalScript_source_lineNumber, [:void, :id, :id, :int] # The first :void indicates a type of return value. # The rest are types of parameters. Then you can call it: [rubyObj evalScript:@"raise Exception" source:@"file" lineNumber:12]; These are similar except objc_method. In this case we need to specify types of parameters. Because all parameters are assumed as an Object (id), but the third parameter is integer, different from an id. So bridge can know the third parameter is not an id, then it converts the parameter to Integer in the ruby level. -- Satoshi Nakagawa On 2007/12/05, at 6:50, Stefan Mueller wrote: > Hello > > I have a question about calling Ruby methods from Objective-C. > > The Introduction from rubycocoa.com only shows you how to do this > for a user-defined method that uses underscores. > > [helper helpfullyAddX:x toY:y] > => > def helpfullyAddX_toY(i,j) > > But how am I supposed to call a function like Object.instance_eval > It takes multiple arguments, and has not enough underscores. > It can also take a single block. > > obj.instance_eval ( aString <, file <, line> > ) > obj.instance_eval ( { block } ) > > The bridge only tries to call the method with the block. > So how do I call the other Version with 2 or 3 params? > > I've made a proxy method that's a better fit for bridge and then > calls the Ruby method, > but I don't think it's ideal. > > def evaluate_source(script, source) > self.instance_eval(script, source) > end > > regards, > Stefan Mueller |