ruby-on-rails – 当用作param时的’*`的含义(不像* arg,只是*)[复制]

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 当用作param时的’*`的含义(不像* arg,只是*)[复制]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > naked asterisk as parameter in method definition: def f(*)1个
当我阅读Rails代码时,我发现了这一点
def save(*)
  create_or_update || raise(RecordNotSaved)
end

*做什么? :o
我知道当我们像* args一样使用它时会发生什么,但在这种情况下,它只是简单*.

ref https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L119

解决方法

在这种特定情况下,save不带任何参数.这就是赤裸裸的啪啪声.但是,您可能知道,在ActiveRecord模型上调用save会接受选项,因为此方法会被ActiveRecord :: Validations覆盖:

https://github.com/rails/rails/blob/v3.1.3/activerecord/lib/active_record/validations.rb#L47

# The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
# replaced with this when the validations module is mixed in,which it is by default.
def save(options={})
 perform_validations(options) ? super : false
end
原文链接:https://www.f2er.com/ruby/270857.html

猜你在找的Ruby相关文章