ruby - ruby代码是如何执行的

标签 ruby interpreter

我最近开始学习 Ruby。我知道 Ruby 是一种解释型语言(尽管“每种”语言都是因为它被 CPU 解释为机器代码)。但是 ruby​​ 解释器如何将用 Ruby 编写的代码转换为机器码呢?我读过解释器不读取源代码,而是字节码,但是我永远不必像在 Java 中那样进行编译。那么,这是 Ruby 为您做的另一件事吗?如果是,它会在运行时生成字节码吗?因为您永远不会像在 Java 中那样获得 .class 文件。最重要的是,我阅读了关于即时编译器的所有内容,这些编译器显然对字节代码做了一些事情,因此它运行得更快。

如果是上述情况,解释器是否首先扫描所有源代码,将其转换为字节码,然后在运行时使用 JIT 再次编译?

最后,我不是在寻找性能方面的答案,我只是想知道它是如何处理的,它经历了哪些阶段,以及它在什么时间进行。

感谢您的宝贵时间。

我正在使用这个解释器 http://www.ruby-lang.org/en/

最佳答案

But how does the ruby interpreter convert the code written in Ruby to machine code?

它没有,至少不是所有的实现。

仅限 Afaik Rubinius正在尝试执行您所描述的操作,即编译为机器代码。

I have read that the interpreter do not read the source code, but byte code, however I do never have to compile as I do in Java. So, is this yet another thing that Ruby does for you?

是的

And if it does, does it generate the byte code at runtime?

是的,差不多。并将其保存在内存中。权衡是下一次它必须重新读取->翻译->执行。

If the above is the case does the interpreter first scan through all of the source code, convert it into byte code and then compiles it another time with JIT at runtime?

不是所有的源代码,只是它所需要的。那么是的,创建一个字节码表示将其保存在内存中,而不一定是编译为机器代码。

关于ruby - ruby代码是如何执行的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068426/

相关文章:

java - Java 解释器是否将字节码文件转换为可执行文件?

ruby-on-rails - Aptana Ruby on Rails 问题,rails : command not found

ruby-on-rails - 我怎样才能使这个方法更简洁?

ruby map !函数和嵌套数组

ruby - 如何转换 slim 到 html?

python - 如何在 python 解释器 shell 中重复最后一个命令?

ruby - method_missing 在 Ruby 中的可见性

if-statement - 解释器,if 语句和 let

python - 在 linux 脚本中是什么意思? #!/usr/bin/python -tt

haskell - Haskell 中 Futamura 投影的证明