1

I see this *MyModel::MY_CONSTANT

and it references this in MyModel:

  MY_CONSTANT = [
    :a_field,
    :another_field,
  [

When called as

permit(:name, *MyModel::MY_CONSTANT)

it expands to

permit(:name, :a_field, :b_field)

but what just happened?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Meltemi
  • 37,979
  • 50
  • 195
  • 293
  • 1
    Probably more than you ever wanted to know about it here: https://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/ – infused Mar 19 '15 at 23:09

1 Answers1

3

I hope that it actually expanded to permit(:name, :a_field, :another_field) :) But yeah, basically that's what it does when used on an array, it takes the values of the array and expands them out as if they were provided as individual arguments. So then you can take those array elements and send them into a method that's expecting individual arguments.

You can also use it in the inverse, you can define a method like:

def foo *args
end

...and when it's called with individual arguments:

foo 'a', 'b', 'c'

...those end up inside the foo method in the array args - this is very handy for wrapping another method, eg.

def some_method *args
  do_something args.first
  super *args   # call the superclass's `some_method` with whatever args it would have received
end

Update

Btw, here's the official docs about this operator.

smathy
  • 26,283
  • 5
  • 48
  • 68