Creating ruby objects in Objective-C
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following: [[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]]; If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker. [RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg]; Are any of those options (or an equivalent) a possibility? Alan
After the Ruby code that defines the class has been evaled, you should be able to do the following: Class rubyClass = NSClassFromString(@"RubyClass"); id rubyObject = [[rubyClass alloc] init]; On Jan 17, 2011, at 2:05 PM, Alan Skipp wrote:
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following:
[[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]];
If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker.
[RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg];
Are any of those options (or an equivalent) a possibility?
Alan _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Brilliant suggestion Eloy, much appreciated. There's a noticeable improvement in speed using this technique. In the method which added ruby objects to an array, 77% of the time was spent on initialising the objects, that's now down to 35%. Alan On 17 Jan 2011, at 13:32, Eloy Durán wrote:
After the Ruby code that defines the class has been evaled, you should be able to do the following:
Class rubyClass = NSClassFromString(@"RubyClass"); id rubyObject = [[rubyClass alloc] init];
On Jan 17, 2011, at 2:05 PM, Alan Skipp wrote:
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following:
[[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]];
If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker.
[RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg];
Are any of those options (or an equivalent) a possibility?
Alan _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
You're welcome Alan :) Hmm, I wonder if this big impact is because of skipping evaling everytime, which iirc is the only thing that evaluateString does, or something else is happening... Have you also tried doing [rubyClass new] instead of alloc init? I thought they should do the same. On 17 jan. 2011, at 15:57, Alan Skipp <al_skipp@fastmail.fm> wrote:
Brilliant suggestion Eloy, much appreciated. There's a noticeable improvement in speed using this technique. In the method which added ruby objects to an array, 77% of the time was spent on initialising the objects, that's now down to 35%.
Alan
On 17 Jan 2011, at 13:32, Eloy Durán wrote:
After the Ruby code that defines the class has been evaled, you should be able to do the following:
Class rubyClass = NSClassFromString(@"RubyClass"); id rubyObject = [[rubyClass alloc] init];
On Jan 17, 2011, at 2:05 PM, Alan Skipp wrote:
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following:
[[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]];
If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker.
[RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg];
Are any of those options (or an equivalent) a possibility?
Alan _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Hi Eloy, The way I'm initializing the ruby objects is with a custom class method as they must be intialized with arguments. For example, here is the macruby class method: def self.initWithPath(path, isActive:active) new(path, active) end Which means that in Objective-C I'm using the somewhat verbose code below: [rubyClass performSelector:@selector(initWithPath:isActive:) withObject:path withObject:[NSNumber numberWithBool:YES]]; If the class could be initialized without arguments then I could use: [[rubyClass alloc] init]; But I've not tried that yet. I imagine it would be a tiny bit faster again, as it doesn't use 'performSelector'. Alan On 17 Jan 2011, at 17:36, Eloy Duran wrote:
You're welcome Alan :)
Hmm, I wonder if this big impact is because of skipping evaling everytime, which iirc is the only thing that evaluateString does, or something else is happening... Have you also tried doing [rubyClass new] instead of alloc init? I thought they should do the same.
On 17 jan. 2011, at 15:57, Alan Skipp <al_skipp@fastmail.fm> wrote:
Brilliant suggestion Eloy, much appreciated. There's a noticeable improvement in speed using this technique. In the method which added ruby objects to an array, 77% of the time was spent on initialising the objects, that's now down to 35%.
Alan
On 17 Jan 2011, at 13:32, Eloy Durán wrote:
After the Ruby code that defines the class has been evaled, you should be able to do the following:
Class rubyClass = NSClassFromString(@"RubyClass"); id rubyObject = [[rubyClass alloc] init];
On Jan 17, 2011, at 2:05 PM, Alan Skipp wrote:
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following:
[[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]];
If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker.
[RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg];
Are any of those options (or an equivalent) a possibility?
Alan _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Hi Alan, When performing Ruby selectors on Ruby objects from Objective-C, the performRubySelector: method should be used instead of performSelector:, because it takes care of special Ruby semantics such as splat or default arguments. [RubyClass performRubySelector:@selector(new:) withArguments: arg, NULL]; Laurent On Jan 17, 2011, at 5:05 AM, Alan Skipp wrote:
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following:
[[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]];
If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker.
[RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg];
Are any of those options (or an equivalent) a possibility?
Alan _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Thanks Laurent, I'd forgotten about performRubySelector:. The ruby class I'm using takes 2 args, one of them a default argument, so I created an initWith method to work around that - problem solved now. Alan On 17 Jan 2011, at 21:51, Laurent Sansonetti wrote:
Hi Alan,
When performing Ruby selectors on Ruby objects from Objective-C, the performRubySelector: method should be used instead of performSelector:, because it takes care of special Ruby semantics such as splat or default arguments.
[RubyClass performRubySelector:@selector(new:) withArguments: arg, NULL];
Laurent
On Jan 17, 2011, at 5:05 AM, Alan Skipp wrote:
Hi, Is there any way to inform an Objective-C class of the existence of a ruby class so that instances can be created in Objective-C code? Currently I am doing the following:
[[MacRuby sharedRuntime] evaluateString:[NSString stringWithFormat:@"RubyClass.new('%@')", arg]];
If I'm adding quite a few objects (300+) to an array in a loop, this line of code is quite slow. My assumption (which may be false) is that either of the 3 options below would be quicker.
[RubyClass performSelector:@selector(new:) withObject:arg]; [RubyClass new:arg]; [[RubyClass alloc] initWithArg:arg];
Are any of those options (or an equivalent) a possibility?
Alan _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (4)
-
Alan Skipp
-
Eloy Duran
-
Eloy Durán
-
Laurent Sansonetti