[英]How do I use Ruby refinements inside Rails Controller/Action?
If I try outside the controller it works: 如果我尝试在控制器之外工作,则可以:
using ParamsExtension
class ApplicationController
If I try inside the controller or an action it does not work: 如果我尝试在控制器内部执行某项操作,或该操作无效:
class ApplicationController
using ParamsExtension
It throws 'undefined method `using'. 它抛出“未定义的方法”使用”。
I read this article and the author is using it inside a class: timelessrepo.com/refinements-in-ruby 我读了这篇文章,作者正在一个类中使用它: timelessrepo.com/refinements-in-ruby
I read the following from the documentation: 我从文档中阅读了以下内容:
You may only activate refinements at top-level, not inside any class, module or method scope.
您只能在顶层激活优化,而不能在任何类,模块或方法范围内激活。 You may activate refinements in a string passed to Kernel#eval that is evaluated at top-level.
您可以激活传递给Kernel#eval的字符串中的优化,该字符串在顶级进行评估。 Refinements are active until the end of the file or the end of the eval string, respectively.
优化有效,直到分别在文件末尾或eval字符串末尾。
http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html
根据本文 ,类内的改进仅在2.3及更高版本上可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.