Answer the question
In order to leave comments, you need to log in
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 method
parameters 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
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)
Probably here, only C will have to be learned =)
https://github.com/ruby/ruby/blob/trunk/method.h
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question