3

In the rails code I came across following method definition def initialize(*)

I understand what def foo(*a) means but can't figure out significance of omitting identifier name after *. How do you access any arguments passed to this method?

saihgala
  • 5,724
  • 3
  • 34
  • 31
  • for variable number of argument. like python – Grijesh Chauhan Feb 27 '13 at 10:40
  • @GrijeshChauhan I understand the concept of variable number of arguments, but how do you access these variable arguments inside `def foo(*)` – saihgala Feb 27 '13 at 10:42
  • Sorry :( my comment was just a guess. I never worked on Ruby. but good question – Grijesh Chauhan Feb 27 '13 at 10:44
  • This is a duplicate of [naked asterisk as parameter in method definition: `def f(*)`](http://stackoverflow.com/questions/5249537/naked-asterisk-as-parameter-in-method-definition-def-f), [What does a single splat/asterisk in a Ruby argument list mean?](http://stackoverflow.com/questions/7531990/what-does-a-single-splat-asterisk-in-a-ruby-argument-list-mean) and [The meaning of `*` when using as a param(not like `*arg`, just `*`)](http://stackoverflow.com/questions/14939344/the-meaning-of-when-using-as-a-paramnot-like-arg-just/14939675#14939675). – Jörg W Mittag Feb 27 '13 at 12:02
  • 1
    @JörgWMittag Thanks, "naked asterisk" term didn't occur to me so couldn't search the web – saihgala Feb 27 '13 at 12:12
  • lolz....... naked asterisk – Gopal S Rathore Feb 27 '13 at 13:09

1 Answers1

3

Here's my guess.

It works because of second line:

def initialize(*)
  super
  ...
end

So the method receives arbitrary number of arguments and passes all of them to super(as you know, super without arguments means take all arguments from original method).

And then in this case the names for arguments are not required.

megas
  • 21,401
  • 12
  • 79
  • 130
  • 2
    It's a safe guard to protect from changing method signatures in your class / module hierarchy: "Do whatever `super` does, I don't care about the arguments." – Koraktor Feb 27 '13 at 11:08