只有*运算符的Ruby方法参数:def save(*).. end

时间:2012-03-10 18:52:51

标签: ruby ruby-on-rails-3

  

可能重复:
  naked asterisk as parameter in method definition: def f(*)
  Accessing a value in a method using *

Rails中的save(*)方法只需要一个星号作为参数。

我知道*可用于*args中的可变长度参数,但*在此上下文中的含义是什么?如何在这里访问参数?

更具体地说,如何将所有参数传递给super来电?

1 个答案:

答案 0 :(得分:1)

在这种特定情况下,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