How does ruby interpreter work




















Languages that use interpreters directly execute code without an intermediate compilation step. This means that Ruby does not first compile a program to an optimized machine language program, which compiled languages such as C , Rust and Go do.

In Ruby, a program is first translated to an instruction set for the Ruby VM, and is then executed immediately after. These instructions are an intermediate step between your Ruby code and the code being executed in the Ruby VM. These instructions make it easier for the Ruby VM to understand Ruby code without having to deal with syntax specific interpretation.

Instruction sequences are optimized operations which represent the interpreted code. Most commands will speak for themselves. The former creates two strings and combines them. The latter creates one string. Of course, this is just about the most basic example we can use, but it shows a good use case of how a small detail in the Ruby language could potentially have a lot of impact:. Over the years since Ruby was first introduced, some of the Ruby implementations that have come into being have been abandoned or discontinued, such as:.

There are a wide variety of runtimes and interpreters to pick from on the Ruby landscape. However, alternate Ruby implementations may very well be the right choice for your project, depending on your functional and technical goals and constraints.

In its role as the reference implementation of Ruby, MRI gets new language features faster, has good enough concurrency and memory stories that are only getting better , and has the widest compatibility with gems some partially written in C. All in all, MRI is a solid, dependable choice for general purpose Ruby code. For larger, enterprise deployments, or for situations where you either need to interact with Java code or other JVM languages or need highly evolved concurrency patterns, JRuby is a compelling option.

And of course, if you have unique needs e. With a wide variety of Ruby runtimes and interpreters to pick from, Ruby shows itself to be a flexible language, useful for a wide array of computing environments, ranging from a corporate big iron Java deployment shop, to software that controls that stoplight in your office you hooked your Raspberry Pi into last weekend.

Picking the right tool for the right purpose is essential, yes, but hopefully this article has shown you that Ruby is so much more than the default Ruby interpreter that came with your OS. The world of Ruby is greatly enhanced by alternative Ruby implementation teams working with the core Ruby MRI team as changes to the language are proposed.

They add diversity to the Ruby implementation community, adding their hard won Ruby implementation experiences and their own perspectives on features going into the language. Ruby enthusiasts collectively owe these teams a great debt of gratitude. Kudos to them for their efforts! Subscription implies consent to our privacy policy.

Thank you! Check out your inbox to confirm your invite. Engineering All Blogs Icon Chevron. Filter by. View all results. Web Front-end. Author Ryan Wilcox. In this post, we will explore each of these steps in detail. First, an example program. These tokens are not guaranteed to be valid Ruby. At this point the stream of tokens could be invalid. It is the responsibility of the parser to determine whether the inputted program is valid or not.

Lets take a look at how the definition of the add method is tokenized using the following code:. This results in the following output:. Each nested array in above output represents a single token. The structure of the array is as follows:. Though the symbol names are not one to one with the C definition inside parse. There are some other differences in the output of ripper and the internal representation of each token, but ripper gives us a good idea of how ruby tokenization works.

As you can see, it returns an array of tokens. The tokenizer is pretty stupid. You can feed it completely invalid Ruby and it will still tokenize it. Lexing is one step beyond tokenization. The string is still broken into tokens, but additional data is added to the tokens.

In the example below we are using Ripper to Lex our small program. There is still no real syntax checking going on at this point. The lexer will happily process invalid code. Now that Ruby has broken up the code into more manageable chunks, it's time for parsing to begin.

During the parsing stage, Ruby transforms the text into something called an abstract syntax tree, or AST. The abstract syntax tree is a representation of your program in memory. You might say that programming languages in general are just more user-friendly ways of describing abstract syntax trees. It might not be easy to read this output, but if you stare at it for long enough you can kind of see how it maps to the original program.

At this point, the Ruby interpreter knows exactly what's you want it to do. It could run your program right now.



0コメント

  • 1000 / 1000