突然想知道,为什么有的语言需要编译之后运行,而有的语言则不需要呢? C, ruby 请分析谢谢!

Python012

突然想知道,为什么有的语言需要编译之后运行,而有的语言则不需要呢? C, ruby 请分析谢谢!,第1张

我们用的大部分程序设计语言都是高级语言,高级语言要执行,必须要先变成计算机能识别的汇编语言.将高级语言变成汇编语言的过程叫做翻译,目前有两种形式的翻译方法,一种是编译,一种是解释.C就属于编译方式,执行前一定先编译一次.而B语言就是解释方式,解释一条执行一条,所以执行先不需要编译一下.

关于 RubyMotion 我已经写过很多文章了,但如何混用Objective-C与Ruby还从未涉及到。实际上你能在RubyMotion项目中使用Objective-C代码,也可以在传统Objective-C的App中使用Ruby代码。也许你一次听来觉得像黑魔法一样,所以来一起看看下面这些示例。

Objective-C in Ruby

很多iOS开发者在现有的Objecive-C代码中存在大量深层的备份日志,这样就为项目手动转换为 RubyMotion 带来了很大的痛苦。然而幸运的是现在仅仅只需要将编译好的Objective-C代码添加到我们的新RubyMotion应用里。

比如我们要在 RubyMotion 应用中使用 Objective-C 形式的 CYAlert 类:

// CYAlert.h

#import <UIKit/UIKit.h>@interface CYAlert : NSObject

+ (void)show@end

// CYAlert.m

#import "CYAlert.h"

@implementation CYAlert

+ (void)show {

UIAlertView *alert = [[UIAlertView alloc] init]

alert.title = @"This is Objective-C"

alert.message = @"Mixing and matching!"

[alert addButtonWithTitle:@"OK"]

[alert show]

[alert release]

}@end

为了在RubyMotion应用中能正常使用,这里需要将CYAlert的两个文件都放置到类似 ./vendor/CYAlert 里面,然后在 Rakefile: 中告诉 RubyMotion 使用vendor_project创建那个文件夹。

Motion::Project::App.setup do |app|

# Use `rake config' to see complete project settings.

app.name = 'MixingExample'

# ./vendor/CYAlert contains CYAlert.h and CYAlert.m

app.vendor_project('vendor/CYAlert', :static)

end

那么现在在 AppDelegate 中,我们这样使用 CYAlert:

class AppDelegate

def application(application, didFinishLaunchingWithOptions:launchOptions)

CYAlert.show

true

end

end