[Turn] on Ruby interpreter: something you need to know

About Ruby interpreter: something you need to know

原文:Ruby Interpreters: What You Need to Know

 

 

Use the correct Ruby interpreter to run programs can play an important role, unfortunately hard to find articles on different Ruby interpreters selected, or how to select the correct interpreter.

Below we compiled a lot of content about the interpreter in Ruby, even if you are at the beginning of this article asked, "What is the Ruby interpreter?", You will understand the knowledge they need to make the right decisions.

 

What is an interpreter? Compiled and interpreted programming language

Language is the basis of communication, which consists of a standard set of vocabulary, syntax and grammar composition, the two agreed to exchange information. But when two people do not say when the same language, you need a way to translate or interpret the same idea from one language into another.

Translation and interpretation of the concept also applies to programming, the machine can not understand their own human level code programming output, which can only execute machine code , which is a limited set of low-level instructions the computer CPU can immediately understand.

The machine has two operating method of programming languages, such as Ruby: compiled and interpreted .

Compiled tool called a language requires a compiler, before the user runs the program, the compiler must be converted into a series of low-level code command, the machine this machine-specific code to run the program.

Interpreted tool requires a programming language interpreter is called, the program runs, progressive interpreter converts the source code to machine code.

This difference is like the difference between translation and foreign language interpreters, translators work will be completed in advance of the need to use translators. Interpreters translated the scene, and people need to listen live.

Compiled language and interpreted language has its advantages and disadvantages, some languages ​​(e.g., Java) using the mixing of the two methods. Usually interpreted language is more flexible, more portable across different systems, and easier to debug. But they pay for these benefits come at the expense of speed and performance.

Compiled language is generally faster than interpreted language, it is because the compiler optimization time to a particular machine running the program code.

While Ruby often interpreted, but it can actually be achieved according to the language interpreted and compiled in the rest of this article, we will discuss only the Ruby interpreter.

 

What is the Ruby interpreter?

Ruby interpreter capable of interpreting a source code program written in Ruby.

Like allowing different people to the same translation or interpretation, it is not a single version of the Ruby interpreter.

Instead, Ruby developers can have many explanations selection. Each language version gives a slightly different "flavor."

So how can you make sure you use the interpreter actually running a "real" Ruby?

By using  Ruby the Spec Suite  (also known as ruby / spec), Ruby Spec Suite is a set of tests used to verify a given Ruby implementation has the correct behavior and results in the interpretation of the program.

 

Ruby interpreter selection: YARV, Ruby MRI, JRuby, and Rubinius

Below, we will discuss the Ruby interpreter of the most popular alternatives: YARV, Ruby MRI / CRuby, JRuby and Rubinius, following all interpreters have passed the Ruby Spec Suite, is a reliable, mature choose to run Ruby code.

YARV

YARV (Yet Another Ruby VM) technology is based on the interpreter stack to support the official Ruby interpreter. Starting Ruby version 1.9, YARV replaced the old version of Ruby MRI (also known as CRuby); starting from Ruby 2.6, YARV that is the official Ruby interpreter.

When you run a Ruby program, YARV codes will be converted to a limited set of instructions running in Ruby virtual machine (VM). VM is a program that runs on your computer, it simulates a separate computer to have better predictability and stability. This ensures that all computers can run Ruby, whether they use a particular kind of machine code.

Ruby MRI/CRuby

In YARV version before Ruby 1.9, the default Ruby interpreter is Ruby MRI, also called CRuby. It represents Ruby MRI Matz's Ruby Interpreter (to "Matz" name, or is the chief designer of Ruby Yukihiro Matsumoto Yukihiro Matsumoto).

CRuby alternative name reflects the fact that the interpreter uses the C programming language. CRuby differs in that the YARV: YARV use the stack to parse Ruby code, while using CRuby abstract syntax tree .

JRuby

As CRuby as in the C language, JRuby is a Java programming language Ruby interpreter. JRuby follow the rest of the  Java Virtual Machine pace programming language running on (JVM), such as  Clojure  and  Scala .

Because using JRuby JVM, so you can run Ruby programs can run anywhere Java program, from laptops to Android phones. JRuby can also use the Java standard library and the three parties.

Rubinius

Rubinius Ruby program tries to interpret as little as C code, different from Ruby MRI / CRuby. Rubinius is the foundation of C ++, while the rest use as much Ruby code.

Rubinius includes a "Just In Time" (JIT) compiler that the program runs compiled code, help it run more dynamically, making it an improvement over Ruby MRI in memory management and speed.

 

Which Ruby interpreter you should use?

Ruby interpreter uses is an open question, depending on your specific situation.

⚠️ (non-original)

Overall performance with emphasis on performance Rubinius or JRuby, default YARV. YARV future performance will be improved, essentially single-threaded multi-threaded instead.

If you have a default version of Ruby installed, you will almost certainly use YARV (if a Ruby 1.9 and later) or Ruby MRI (if a Ruby 1.8 and earlier). In many cases, the default selection is sufficient to meet the needs of Ruby developers.

However, if your Ruby program with high performance requirements, Rubinius or JRuby might be better. According to a benchmark , Rubinius and JRuby's performance is about twice Ruby MRI's.

Ruby interpreter Another factor to consider is the use of threads and concurrency issues. Ruby MRI using called global interpreter lock (GIL). GIL prevents multiple program threads simultaneously access the same data. By default, Ruby is a single-threaded, multi-threaded and does not allow parallel.

However, this limitation does not exist in other implementations of the interpreter Ruby, e.g. Rubinius and JRuby, you can take advantage of multi-core and multi-CPU system, significantly improved performance.

The good news is, Ruby team hopes mechanism called Guilds by GIL to bypass restrictions in the upcoming release of version 3 in Ruby. In the future you may see a number of parallel and multi-threaded change for the better situation in the default version of Ruby. But you must be patient and wait for Ruby 3 (there is no clear release date).

 

At last

Alternatives listed above are by no means the only alternative Ruby implementations. mruby  is a Ruby implementation for  Raspberry Pi  and other embedded systems. While  Opal  is a convert Ruby to JavaScript compiler.

However, for most Ruby scene, choose the following can not be wrong:

The default Ruby implementations, YARV or Ruby MRI, may be suitable for the needs of many Ruby programmers. It is the fastest iteration of the new Ruby language features, and with different  Ruby gems  have maximum compatibility. However, it currently lacks true parallelism and speed may be slower than the other interpreter.

When you need to run Ruby on many different systems (including corporate computer), JRuby is a good choice. JRuby can also easily interact with Java code.

When you need very high performance, or when you want to parallelism, Rubinius is a good choice. It also enables you to use C or C ++ code that is connected Ruby.

 

Guess you like

Origin www.cnblogs.com/chentianwei/p/11430130.html