V
V
Viktor Vsk2014-08-01 18:41:47
ruby
Viktor Vsk, 2014-08-01 18:41:47

How does the Ruby interpreter work?

As you know, in Ruby, <=>, [], + and other "operations" are methods that can be overridden.
How does the interpreter understand that methodparameters can be passed to using method(params), and in the case of the [] method, it "understands" that the parameters are between square brackets and round brackets can no longer be used. Similarly, how does one parse that method = 1 is the same as method= 1?
Where is the best place to read more about such details? How much sugar of this kind is in the tongue?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
vsuhachev, 2014-08-24
@viktorvsk

This is defined by the ruby ​​syntax and is not changeable.
In more detail, first the source text is parsed into an intermediate representation of the code. After that, individual characters '[' and ']' cease to exist, the code becomes a tree structure in memory, which already stores entire operators in nodes. And accordingly, by redefining the :[] operator, you really just shuffle the pointers in this structure, which knows nothing about the syntax of the input language (in our case, ruby)

S
Shetani, 2014-08-01
@Shetani

Probably here, only C will have to be learned =)
https://github.com/ruby/ruby/blob/trunk/method.h

S
Sergey, 2014-08-01
Protko @Fesor

It all collapses at runtime. That is, until the moment the function (operator) is directly called, it is not known what exactly will be called. The analysis of the source codes, I dare to assume, is organized, like everyone else, by means of AST.

M
Maxim, 2014-08-02
@1kachan

mb late binding mechanism?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question