加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 直接访问受保护方法和使用send之间的区别

发布时间:2020-12-17 03:03:56 所属栏目:百科 来源:网络整理
导读:为什么以下代码中的两种类型的调用方法之间存在差异: class Access def method_missing name print "Method is missing" end protected def protected_method print "Calling Protected Method" endendaccess = Access.newaccess.protected_method #Method
为什么以下代码中的两种类型的调用方法之间存在差异:

class Access
    def method_missing name
        print "Method is missing"
    end

    protected
    def protected_method
        print "Calling Protected Method"
    end
end

access = Access.new
access.protected_method #Method is missing
access.send(:protected_method) #Calling Protected Method

access.protected_method按预期工作.但send选项尝试调用该方法,即使它受到保护.我想知道内部发生了什么.

我得到一个要调用的方法的字符串,所以我想使用send但我不想调用受保护的方法.

解决方法

这就是发送的方式.如果您只想调用公共方法,请使用public_send.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读