0

After reading through documentation, and reading some examples, I'm still not satisfied with exactly the exact usage of the splat operator *. When is it good to use? When is it bad to use? I can find some uses of it online on documentation, but I can't seem to find any simple examples illustrating what good or bad it can cause. Thanks'

Muntasir Alam
  • 1,777
  • 2
  • 17
  • 26
  • Good or bad? There is no good or bad. Use `*` when you want to gather parameters to a method into a single variable, or to explode an array into its constituent elements, perhaps as parameters to a method. See http://stackoverflow.com/q/918449/128421. – the Tin Man Jun 29 '16 at 00:43
  • 1
    This is a good question, but perhaps not for SO, as it is open-ended and cannot be answered objectively. – Cary Swoveland Jun 29 '16 at 00:57
  • ok ill take a look at the link. thanks – Muntasir Alam Jun 29 '16 at 00:59

1 Answers1

1

I can show you a few examples of how it's used.

Say you have a method which takes a few arguments

def foo(a,b,c)

And you want to call this method, provide the values for arguments using an array. You can write:

foo(*[1,2,3])

Another situation is you want to monkey patch a record but don't want to break the original functionality. For example, overwriting save in a rails model:

def save(*args)
  # do something custom here
  super(*args)
end

This is to say "I don't care about the arguments to this function, but I want to make sure they are all passed to the super call. "

max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • 1
    How does #2 differ from `super` without arguments? Another would be `[*[1,2], *[3,4]] #=> [1, 2, 3, 4]`. – Cary Swoveland Jun 29 '16 at 01:13
  • In this particular example, the `save` method in ActiveRecord is called under-the-hood by a number of other methods. I'm not aware of what arguments they're passing. So to make sure that whatever arguments they're providing don't get ignored, I use `super(*args)`. super won't magically pass arguments when it's invoked, they have to be manually specified. – max pleaner Jun 29 '16 at 03:25
  • 2
    I don't know Rails. In pure Ruby `super` without an argument passes all arguments and, if there is one, a block. Does Rails override that behaviour? – Cary Swoveland Jun 29 '16 at 03:48
  • I'm talking about Ruby, I suppose I could be mistaken. Anyway, it doesn't break anything. – max pleaner Jun 29 '16 at 04:09
  • No, nothing is broken (unless a block needs to be passed), but the application is not a good example of the use of a splat, considering that the same can be done by invoking `super` with no parens and no arguments, which is the customary way of doing that. – Cary Swoveland Jun 29 '16 at 04:19
  • @CarySwoveland I think max example was about being able to add arguments to the existing method without breaking existing functionality. So say `save` method originally takes 1 argument and there is a bunch of save('me') calls in your code. Then you decide that you sometimes want to call it with two arguments - `save('me', 'plz')`. Monkeypatching the method with the splat operator would allow you to do this without throwing an error for calls with one argument. – ivanibash Oct 01 '17 at 22:44