如何在 MacRuby 中使用 Obj-C 类

发布于 2024-08-25 15:12:59 字数 255 浏览 6 评论 0原文

我试图在我的 MacRuby 项目中使用外部 Obj-C 类,但我不知道如何导入它。具体来说,我想在 MacRuby 0.5 项目中使用 ObjectiveResource(因为 ActiveResource 还不能工作)。

我已经了解了 MacRuby 中的“framework”命令,但它似乎只适用于实际的框架。

问题:我应该在哪里安装 Objective-resource 目录?如何将这些类提取到我的 ruby​​ 代码中?

感谢您的帮助!

I am trying to use an external Obj-C class in my MacRuby project, but I can't figure out how to import it. Specifically, I want to use ObjectiveResource inside a MacRuby 0.5 project (since ActiveResource doesn't work - yet).

I have gotten as far as the 'framework' command in MacRuby, but it only seems to apply to actual frameworks.

Questions: where should I install the objective-resource directory? how do I pull these classes inside my ruby code?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

是你 2024-09-01 15:12:59

您可以使用 ClassName.new 直接访问该类。

如果您的项目中有一个名为 Utilities 的类,并且它有一个名为greeting: 的方法,您可以像这样调用它。

util = Utilities.new
puts util.greeting("Good morning")

不需要任何要求或框架声明。非常简单不是吗。我在 MacRuby 上观看 Peepcode 截屏视频时发现了这一点。

You can access the class directly using ClassName.new.

If you have a class named Utilities in your project and it has a method named greeting:, you would call it like so.

util = Utilities.new
puts util.greeting("Good morning")

There is no require or framework declarations required. Amazingly simple isn't it. I discovered this watching the Peepcode screencast on MacRuby.

中性美 2024-09-01 15:12:59

另外,如果您有兴趣从 Objective-c 调用 Ruby 代码,这里是 MacRuby 邮件列表的摘录:

MacRuby 运行时初始化后,您可以从 Ruby 访问所有 Objective-C 对象。例如,如果您有一个名为 Foo 的 Objective-C 类,则可以执行 `Foo.new' 等。

另一种可能性是使用 [NSObject PerformRubySelector:]< 将您的 Objective-C 对象传递给 Ruby 方法/代码>。

ruby:

class Foo
  def test(o)
    o.something
  end
end

objc:

MyObject *o = [MyObject new]; // where o responds to -something
MacRuby *runtime = [MacRuby sharedRuntime]; 
id foo_obj = [runtime evaluateString:@"new Foo"];
[foo_obj performRubySelector:@selector(test:) withArguments: o, NULL];

在此处查看整个 API: http ://www.macruby.org/trac/browser/MacRuby/trunk/include/ruby/objc.h

主题:http://lists.macosforge.org/pipermail/macruby-devel/2010-April/004715.html

(编辑)也就是说,也许最简单的方法是为您的 ruby​​ 文件添加一个头文件,其中包含等效方法。

假设您有这个 ruby​​ 类:

class Foo
  def bar(moo)
    "a string like #{moo}"
  end
end

因此等效的头文件将类似于:

@interface Foo : NSObject
- (NSString *)bar:(NSString *)moo
@end

现在,只需 #import 标头并使用您的 ruby​​ 类作为 Obj-C 中的标准 Obj-C 类代码:

NSString *aString;
Foo *myFoo;
aString = [myFoo bar:@"me"];

这可能看起来很奇怪,但它确实有效。本质上,这个想法是闭上编译器的嘴,让它编译你的代码,而不会抱怨缺少方法。然后,在运行时,它就会正常工作(我想这要归功于 Obj-C 本身的动态特性)。顺便说一句,为了模仿 ruby​​ 无类型行为,id 是您在头文件中编写方法签名的朋友。

Also, if you are interested in calling Ruby code from Objective-c, here is an extract from the MacRuby mailing list:

Once the MacRuby runtime is initialized, you can access all your Objective-C objects from Ruby. For example, if you have an Objective-C class named Foo, you can do `Foo.new', etc.

Another possibility is to pass your Objective-C objects to a Ruby method by using [NSObject performRubySelector:].

ruby:

class Foo
  def test(o)
    o.something
  end
end

objc:

MyObject *o = [MyObject new]; // where o responds to -something
MacRuby *runtime = [MacRuby sharedRuntime]; 
id foo_obj = [runtime evaluateString:@"new Foo"];
[foo_obj performRubySelector:@selector(test:) withArguments: o, NULL];

Check out the whole API here: http://www.macruby.org/trac/browser/MacRuby/trunk/include/ruby/objc.h

Thread: http://lists.macosforge.org/pipermail/macruby-devel/2010-April/004715.html

(Edit) That said, perhaps the easiest way is to add a header file for your ruby file, containing your methods equivalents.

Suppose that you have this ruby class:

class Foo
  def bar(moo)
    "a string like #{moo}"
  end
end

so the equivalent header file will be something like that:

@interface Foo : NSObject
- (NSString *)bar:(NSString *)moo
@end

Now, just #import the header and use your ruby classes as standard Obj-C ones from within your Obj-C code:

NSString *aString;
Foo *myFoo;
aString = [myFoo bar:@"me"];

It may seem weird, but it works. Essentially, the idea is to shut the compiler's mouth and let it compile your code without complaining about missing methods. Then, at runtime, it will just work (I suppose thanks to the dynamic nature of Obj-C itself). By the way, to mimic the ruby typeless behavior, id is your friend in writing methods signatures in the header file.

毁我热情 2024-09-01 15:12:59

您是否看过这个创建 ObjC 的示例捆?

Did you look at this example of creating an ObjC bundle?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文