用作参数时`*`的含义(不像*arg,只是*) [英] The meaning of `*` when using as a param(not like *arg, just *)
问题描述
当我在阅读 Rails 代码时,我发现了这个
When I was reading Rails code, I found this
def save(*)
create_or_update || raise(RecordNotSaved)
end
*
有什么作用?:O我知道当我们像 *args
一样使用它时会发生什么,但在这种情况下,它只是普通的 *
.
What does the *
do? :O
I know what happens when we use it like *args
, but in this case, it's just plain *
.
ref https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L119
推荐答案
在这种特定情况下, save 不接受任何参数.这就是赤裸裸的啪啪会发生的事情.但是,正如您可能知道的,在 ActiveRecord
模型上调用 save 接受选项,因为此方法在此处被 ActiveRecord::Validations
覆盖:
In this specific case, save doesn't take any arguments. That's what happens with a naked splat. But, as you may be aware, calling save on an ActiveRecord
model accepts options because this method gets overridden by ActiveRecord::Validations
here:
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
这篇关于用作参数时`*`的含义(不像*arg,只是*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!