alste
alste

Reputation: 1455

Executing Objective-C from Ruby on Rails

I want to execute the following Objective-C code in my Rails application:

CFMutableStringRef inputString = CFStringCreateMutableCopy(kCFAllocatorDefault, 32,     CFSTR("общей"));
CFLocaleRef locale = CFLocaleCreate(kCFAllocatorDefault, CFSTR("ru"));

CFStringTransform(inputString, NULL, kCFStringTransformStripDiacritics, false);
CFStringLowercase(inputString, locale);


NSLog(@"%@", (NSString *)inputString);

CFRelease(locale);
CFRelease(inputString);

It basically outputs a lowercase, diacritics-free version of the input string. I am running on a Snow Leopard server.

How can I do this (without using MacRuby, which seems to be an overkill here)? I've heard of Ruby extensions but can't find any resources in my case.

Upvotes: 4

Views: 637

Answers (2)

rob mayoff
rob mayoff

Reputation: 385560

You can probably do it using ffi, but it'll require entering a bunch of function specs. Tiny test case for calling a Core Foundation function:

require 'ffi'

module CF
  extend FFI::Library
  ffi_lib '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'
  attach_function :CFAbsoluteTimeGetCurrent, [], :double
end

puts CF.CFAbsoluteTimeGetCurrent

Upvotes: 1

Francisco Soto
Francisco Soto

Reputation: 10392

Instead of creating an app you need to be spawning on and off in a single Rails instance you can write your app using other ways for communication, like named pipes:

http://hints.macworld.com/article.php?story=20041025103920992 http://www.pauldix.net/2009/07/using-named-pipes-in-ruby-for-interprocess-communication.html

Or going the Ruby C extensions way:

http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html http://people.apache.org/~rooneg/talks/ruby-extensions/ruby-extensions.html

Upvotes: 0

Related Questions