What does send() do in Ruby?

后端 未结 6 1007
野性不改
野性不改 2020-11-28 01:49

Can someone please tell me what

send(\"#{Model.find...}\")

is and does?

6条回答
  •  情深已故
    2020-11-28 02:26

    send sends a message to an object instance and its ancestors in class hierarchy until some method reacts (because its name matches the first argument).

    Practically speaking, those lines are equivalent:

    1.send '+', 2
    1.+(2)
    1 + 2
    

    Note that send bypasses visibility checks, so that you can call private methods, too (useful for unit testing).


    If there is really no variable before send, that means that the global Object is used:

    send :to_s    # "main"
    send :class   # Object
    

提交回复
热议问题