ruby-on-rails – 什么是attr_internal用于

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 什么是attr_internal用于前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 ruby on rails的新手,同时通过rails代码我找到了
attr_internal :response_body
  attr_internal :action_name
  attr_internal :formats

它是做什么的?为什么他们使用attr_internal为响应主体或动作名称分配行动?

解决方法

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

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)

这里的伎俩

原文链接:https://www.f2er.com/ruby/270109.html

猜你在找的Ruby相关文章