Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is ruby code executed

I have recently started to learn Ruby. I know that Ruby is a interpreted language(even though "every" language is since it is interpreted by the CPU as machine code). But how does the ruby interpreter convert the code written in Ruby to machine code? 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? Because you never get a .class file as you do in Java. 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.

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?

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/

like image 889
user626912 Avatar asked Feb 21 '11 16:02

user626912


1 Answers

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

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.

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?

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?

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.

like image 159
OscarRyz Avatar answered Nov 15 '22 16:11

OscarRyz