简体   繁体   English

如何执行ruby代码

[英]How is ruby code executed

I have recently started to learn Ruby. 我最近开始学习Ruby。 I know that Ruby is a interpreted language(even though "every" language is since it is interpreted by the CPU as machine code). 我知道Ruby是一种解释型语言(尽管“每种”语言都是因为它被CPU解释为机器代码)。 But how does the ruby interpreter convert the code written in Ruby to machine code? 但ruby解释器如何将用Ruby编写的代码转换为机器代码? 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. 我已经读过,解释器不会读取源代码,而是读取字节码,但是我从来没有像在Java中那样编译。 So, is this yet another thing that Ruby does for you? 那么,这是Ruby为你做的另一件事吗? And if it does, does it generate the byte code at runtime? 如果确实如此,它是否在运行时生成字节代码? Because you never get a .class file as you do in Java. 因为你永远不会像在Java中那样获得.class文件。 And on top of it all I read about Just-In-Time compilators that obviously does something to the byte code so it runs faster. 最重要的是,我读到了Just-In-Time编译器,它显然对字节代码做了一些事情,因此它运行得更快。

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

And last I AM NOT looking for an answer with the performance aspect of this, I just want to know how it is processed, which stages it goes through and in what time it does so. 最后我不是在寻找有关性能方面的答案,我只是想知道它是如何处理的,它经历了哪些阶段以及它在什么时候这样做。

Thanks for your time. 谢谢你的时间。

I am using this interpeter http://www.ruby-lang.org/en/ 我正在使用这个插件http://www.ruby-lang.org/en/

But how does the ruby interpreter convert the code written in Ruby to machine code? 但ruby解释器如何将用Ruby编写的代码转换为机器代码?

It doesn't, at least not all the implementations. 它没有,至少不是所有的实现。

Afaik only Rubinius is trying to do what you describe, that's compiling 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. 我已经读过,解释器不会读取源代码,而是读取字节码,但是我从来没有像在Java中那样编译。 So, is this yet another thing that Ruby does for you? 那么,这是Ruby为你做的另一件事吗?

Yes

And if it does, does it generate the byte code at runtime? 如果确实如此,它是否在运行时生成字节代码?

Yeap, pretty much. 是的,差不多。 And keeps it in memory. 并将其保存在记忆中。 The tradeof is the next time it has to read->translate->execute all over again. 交易是下次必须阅读 - >翻译 - >重新执行。

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

Not all the source code, just what it needs. 不是所有的源代码,只是它需要的东西。 Then yes, create a bytecode representation keeps it in memory, and not necessarily that is compiled to machine code. 然后是的,创建一个字节码表示将其保存在内存中,而不一定是编译为机器代码。

The standard implementation of Ruby1.8 uses an interpreter called MRI (Matz's Ruby Interpreter). Ruby1.8的标准实现使用称为MRI的解释器(Matz的Ruby解释器)。 This is a program that is compiled to machine code that: 这是一个编译为机器代码的程序:

  1. Reads the text files into a data structure. 将文本文件读入数据结构。
  2. Follows the instructions in the data structure to decide what to do 按照数据结构中的说明来决定要执行的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM