8

When I was reading Rails code, I found this

def save(*)
  create_or_update || raise(RecordNotSaved)
end

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

Takehiro Adachi
  • 954
  • 1
  • 9
  • 22
  • 2
    http://stackoverflow.com/questions/7531990/what-does-a-single-splat-asterisk-in-a-ruby-argument-list-mean – bullfrog Feb 18 '13 at 15:28

2 Answers2

8

It means the same thing as it does when used with a parameter name: gobble up all the remaining arguments. Except, since there is no name to bind them to, the arguments are inaccessible. In other words: it takes any number of arguments but ignores them all.

Note that there actually is one way to use the arguments: when you call super without an argument list, the arguments get forwarded as-is to the superclass method.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
2

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
Learner
  • 4,596
  • 1
  • 20
  • 23
  • i c! :D Does the `ActiveRecord::Validations` overrides `save` because its included after `ActiveRecord::Persistence` in `ActiveRecord::Base` ? :O – Takehiro Adachi Feb 18 '13 at 15:30