I downloaded the early release of MacRuby hoping to integrate it into an existing cocoa application: I need to be able to run small amounts of user-supplied code to do custom string manipulations, and I thought MacRuby might be perfect for this. However, I don't see any obvious way to hand off an NSString with code and have it execute in some environment. Does such an API exist? The only entry point I saw was macruby_main(), which doesn't seem quite right, or ruby_exec_node() which looks pretty low-level. I realize that MacRuby is still in an early state, but I just wanted to be sure I wasn't overlooking something before I dug too much deeper. thanks, Niko
I downloaded the early release of MacRuby hoping to integrate it into an existing cocoa application: I need to be able to run small amounts of user-supplied code to do custom string manipulations, and I thought MacRuby might be perfect for this. However, I don't see any obvious way to hand off an NSString with code and have it execute in some environment. Does such an API exist? The only entry point I saw was macruby_main(), which doesn't seem quite right, or ruby_exec_node() which looks pretty low-level. I realize that MacRuby is still in an early state, but I just wanted to be sure I wasn't overlooking something before I dug too much deeper.
Once string is hooked up, I suppose you could do [RbKernel eval:@"…"] -Ben
Hi, On Mar 18, 2008, at 2:14 PM, Niko Matsakis wrote:
I downloaded the early release of MacRuby hoping to integrate it into an existing cocoa application: I need to be able to run small amounts of user-supplied code to do custom string manipulations, and I thought MacRuby might be perfect for this. However, I don't see any obvious way to hand off an NSString with code and have it execute in some environment. Does such an API exist? The only entry point I saw was macruby_main(), which doesn't seem quite right, or ruby_exec_node() which looks pretty low-level. I realize that MacRuby is still in an early state, but I just wanted to be sure I wasn't overlooking something before I dug too much deeper.
At the moment the only thing you can do is to use the C APIs, which are not really user-friendly. We could introduce a convenience facility to evaluate code. NSNumber *number = [@"1+2+3+4+5" evaluateAsRubyExpression]; (Ideas?) Or directly calling Kernel#eval from Objective-C, as Ben said, which should work in theory, though I didn't try it yet. Laurent
However, I don't see any obvious way to hand off an NSString with code and have it execute in some environment. Does such an API exist? At the moment the only thing you can do is to use the C APIs, which are not really user-friendly.
We could introduce a convenience facility to evaluate code.
NSNumber *number = [@"1+2+3+4+5" evaluateAsRubyExpression];
This is OK, but I’m not too keen on adding the extension method when there’s already something else that works.
Or directly calling Kernel#eval from Objective-C, as Ben said, which should work in theory, though I didn't try it yet.
This will be easier when strings bridge properly. It seems like the cleanest way to me. You can even make a new proc and then call it over and over again. RbObject *squareIt = [RbKernel eval:@"Proc.new {|n| n*n}"]; NSNumber *twentyFive = [squareIt call:[NSNumber numberWithInt:5]]; All this being said, it’d be nice to have a real quick way to boot up an embedded MR for “normal” Objective-C apps. -Ben
On Tue, Mar 18, 2008 at 3:16 PM, Benjamin Stiglitz <ben@tanjero.com> wrote:
However, I don't see any obvious way to hand off an NSString with code and have it execute in some environment. Does such an API exist?
At the moment the only thing you can do is to use the C APIs, which are not really user-friendly.
We could introduce a convenience facility to evaluate code.
NSNumber *number = [@"1+2+3+4+5" evaluateAsRubyExpression];
This is OK, but I'm not too keen on adding the extension method when there's already something else that works.
Or directly calling Kernel#eval from Objective-C, as Ben said, which should work in theory, though I didn't try it yet.
This will be easier when strings bridge properly. It seems like the cleanest way to me. You can even make a new proc and then call it over and over again.
RbObject *squareIt = [RbKernel eval:@"Proc.new {|n| n*n}"]; NSNumber *twentyFive = [squareIt call:[NSNumber numberWithInt:5]];
In theory this should work already, MacRuby will (temporarily) convert the NSString into a Ruby String. In practice it may not work because of bugs :) Note that the prefix is RB not Rb, and that it's only included when there is already an Objective-C class with that name. (Because when you define "Foo" in Ruby, you expect it to be "Foo" in IB.)
All this being said, it'd be nice to have a real quick way to boot up an embedded MR for "normal" Objective-C apps.
I will write a sample that illustrates that. Laurent
participants (4)
-
Benjamin Stiglitz
-
Laurent Sansonetti
-
Laurent Sansonetti
-
Niko Matsakis