attr_internal 有什么用 [英] what is attr_internal used for

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

问题描述

我是 ruby​​ on rails 的新手,而通过 rails 代码我发现

I am new in ruby on rails while go through rails code I found that

   attr_internal :response_body
  attr_internal :action_name
  attr_internal :formats

它是做什么的?为什么他们使用 attr_internal 为响应正文或操作名称进行操作调度?

what is it what does it do ? why they used attr_internal for action dispatch for response body or action name?

推荐答案

它声明了一个由内部命名的实例变量支持的属性读取器和写入器.

It declares an attribute reader and writer backed by an internally-named instance variable.

http://rubydoc.info/docs/rails/Module:attr_internal 看看进入这个

查看主文件

https://github.com/rails/docrails/blob/master/activesupport/lib/active_support/core_ext/module/attr_internal.rb

 def attr_internal_define(attr_name, type)
  internal_name = attr_internal_ivar_name(attr_name).sub(/\A@/, '')
  class_eval do # class_eval is necessary on 1.9 or else the methods a made private
    # use native attr_* methods as they are faster on some Ruby implementations
    send("attr_#{type}", internal_name)
  end
  attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer
  alias_method attr_name, internal_name
  remove_method internal_name
end


 # use native attr_* methods as they are faster on some Ruby implementations
  send("attr_#{type}", internal_name)

技巧在这里

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

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