What is the meaning of the send keyword in Ruby AST?

I am trying to study the vocabulary and parser of Ruby ( whitequark parser ) to learn more about the procedure, to further generate machine code from a Ruby script.

When parsing the next line of Ruby code.

def add(a, b)
    return a + b
end

puts add 1, 2

This leads to the following notation for the S-expression.

s(:begin,
    s(:def, :add,
        s(:args,
            s(:arg, :a),
            s(:arg, :b)),
        s(:return,
            s(:send,
                s(:lvar, :a), :+,
                s(:lvar, :b)))),
    s(:send, nil, :puts,
        s(:send, nil, :add,
            s(:int, 1),
            s(:int, 3))))

Can someone explain to me the definition of a keyword : send in the resulting notation S-expressions?

+4
source share
2 answers

Ruby is built on top of the "everything is an object" paradigm. However, everything, including numbers, is an object.

, , , :

3.14.+(42)
#⇒ 45.14

, Ruby 3.14 + 42. , , Object#send:

3.14.send :+, 42
#⇒ 45.14

: " :+ [s] (42) 3.14."

+4

Ruby - - . - , . ,

foo.bar(baz)

, self bar , foo, , baz . (, foo baz , , Ruby , self , . , , .)

:

a + b

+ a, b

puts add 1, 2

add self 1 2 , puts self, send .

, Object#send/Object#public_send. , , . , , , AST. . Object#send ( Ruby monkey-patching Object#send, , ), Object#send, .

+4

Source: https://habr.com/ru/post/1678306/


All Articles