send() 在 Ruby 中有什么作用? [英] What does send() do in Ruby?

查看:30
本文介绍了send() 在 Ruby 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我什么

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

是和确实?

推荐答案

send 向对象实例及其在类层次结构中的祖先发送消息,直到某些方法做出反应(因为它的名称与第一个参数匹配)).

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

请注意,send 绕过了可见性检查,因此您也可以调用私有方法(对于单元测试很有用).

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

如果send前真的没有变量,说明使用的是全局Object:

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

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

这篇关于send() 在 Ruby 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆